angularjs比较两个数组 [英] angularjs compare two arrays

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

问题描述

有,我想知道我怎么能在angularjs比较两个数组,并返回值,如果是有道理的。这里是第一个:

  [{ID:2,student_name:丽莎,外星人:A,世界:太阳,理由:我像这样送one\",\"submit_time\":null},{\"id\":1,\"student_name\":\"Liz\",\"alien\":\"B\",\"world\":\"Earth\",\"justification\":null,\"submit_time\":\"2014-09-25T08:37:34.526-05:00\"}]

下面是第二个

<$p$p><$c$c>[{\"id\":1,\"alien\":\"A\",\"world\":\"Sun\",\"color\":\"red\"},{\"id\":2,\"alien\":\"A\",\"world\":\"Mercury\",\"color\":\"red\"},{\"id\":3,\"alien\":\"B\",\"world\":\"Earth\",\"color\":\"red\"},{\"id\":4,\"alien\":\"B\",\"world\":\"Moon\",\"color\":\"red\"},]

所以我要检查,如果在外星人全球的价值在这两个阵列匹配,那么我可以在颜色从第二阵列值

下面是code我把控制器:

  angular.forEach(ARR1,函数(值,键1){
    angular.forEach(ARR2,功能(值2,密钥2){
        如果(value1.alien === value2.alien&放大器;&安培; value1.world === value2.world){
            的console.log(value2.color);        }
    });
});

我用普通angular.forEach?我怎样才能做到这一点?和我在哪里存储颜色值?

在此先感谢!


解决方案

像邓肯说,这些对象的两个数组,而不是多维数组。这里我用angulars .forEach方法来遍历两个数组然后比较对象的属性。

我添加了一个评论,你会得到你的配色。

  angular.forEach(ARR1,函数(值,键1){
    angular.forEach(ARR2,功能(值2,密钥2){
        如果(value1.alien === value2.alien&放大器;&安培; value1.world === value2.world){
            //在这里你抢value2.color
        }
    });
});

下面是一个小提琴

there, I am wondering how I can compare two arrays in angularjs, and return the value if that makes sense. Here is the first one:

[{"id":2,"student_name":"LiSa","alien":"A","world":"Sun","justification":"i like sent this one","submit_time":null},{"id":1,"student_name":"Liz","alien":"B","world":"Earth","justification":null,"submit_time":"2014-09-25T08:37:34.526-05:00"}]

Here is the second one:

[{"id":1,"alien":"A","world":"Sun","color":"red"},{"id":2,"alien":"A","world":"Mercury","color":"red"},{"id":3,"alien":"B","world":"Earth","color":"red"},{"id":4,"alien":"B","world":"Moon","color":"red"},]

So I want to check if the alien and world's value are matching in these two array, then i can get the color value from the second array.

Here is the code I put in the controller:

    angular.forEach(arr1, function(value1, key1) {
    angular.forEach(arr2, function(value2, key2){
        if(value1.alien === value2.alien && value1.world === value2.world){
            console.log(value2.color);

        }
    });
});

Shall I use angular.forEach? How can i do that? and where do I store the color value?

Thanks in advance!

解决方案

Like duncan said, these are both arrays of objects and not multi-dimensional arrays. Here I use angulars .forEach method to loop through both arrays then compare the object properties.

I've added a comment where you would get your matching color.

angular.forEach(arr1, function(value1, key1) {
    angular.forEach(arr2, function(value2, key2) {
        if (value1.alien === value2.alien && value1.world === value2.world) {
            // here is where you grab the value2.color
        }
    });
});

Here's a fiddle

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

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