加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs比较两个数组

发布时间:2020-12-17 09:57:23 所属栏目:安全 来源:网络整理
导读:我如何比较AngularJS中的两个数组,并返回匹配的值? 这是第一个数组: [{ "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
我如何比较AngularJS中的两个数组,并返回匹配的值?

这是第一个数组:

[{
    "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"
}]

这是第二个:

[{
    "id":1,"color":"red"
},{
    "id":2,"world":"Mercury",{
    "id":3,{
    "id":4,"world":"Moon","color":"red"
}]

我想检查外星人和世界的值是否在这两个数组中匹配.然后我可以从第二个数组中获取颜色值.

这是我放在控制器中的代码:

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);

        }
    });
});

我应该使用angular.forEach吗?我怎样才能做到这一点?我在哪里存储颜色值?

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

我添加了一条评论,您可以获得匹配的颜色.

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

这是一个fiddle

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读