如何使用angular.foreach比较两个JavaScript数组 [英] how to compare two javascript array using angular.foreach

查看:43
本文介绍了如何使用angular.foreach比较两个JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个javascript对象

I have two javascript Object

    var obj1= {
           key1:'value1',
           key2:'value2'
    };

还有

 var obj2= {
           key1:'value1',
           key2:'someOtherValue'
    };

如您所见, key2 上的两个对象都有一个黑白区别,我想要一个有角度的foreach循环,它可以检查两个对象并可以返回控制台消息"Difference at key2".我已经尝试过有角度的foreach,但是它不允许多个对象,所以我应该如何比较?

As you can see there is one difference b/w both objects at key2, i want a angular foreach loop which can check both objects and can return a console message "Difference is at key2". I already tried angular foreach but it doesn't allow more than one object so how should i compare?

推荐答案

这是带有angular foreach循环的纯angularjs代码.

Here is pure angularjs code with angular foreach loop.

var keepGoing = true;
angular.forEach(obj1, function(value, key){
    angular.forEach(obj2, function(value2, key2){
        if(keepGoing) {
            if(value == value2){
                keepGoing = true;
            }
            else{
                console.log('Difference is at', key2)
                keepGoing = false;
            }
        }
    })
})

这篇关于如何使用angular.foreach比较两个JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆