javascript - 比较 angularJs 中的两个数组

标签 javascript angularjs

我有两个这样的数组:

var friendemail  = [ {
    "data": {
        "status": "OK",
        "message": "User list fetched successfully",
        "userList": [
            {
                "userId": 1,
                "emailId": "abc.com",
                "firstName": "Abc",
                "lastName": "Xyz",
                "nickName": "Nic",
                "type": "USER",
                "apiKey": "355901361"
            },
            {
                "userId": 2,
                "emailId": "babitadhami@g.com",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 3,
                "emailId": "testuser@g.com",
                "firstName": "Test_User",
                "lastName": "Account",
                "nickName": "Testi",
                "type": "USER",
                "apiKey": "1403626362113"
            },
            {
                "userId": 4,
                "emailId": "dhami@gmail.com",
                "firstName": "dhami",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "DEVELOPER",
                "apiKey": "222234567"
            },
            {
                "userId": 5,
                "emailId": "babita.dhami@g.com",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "1403709178117"
            },
            {
                "userId": 6,
                "emailId": "Chris@abc.com",
                "firstName": "dhami",
                "lastName": "dhami",
                "nickName": "dhami",
                "type": "DEVELOPER",
                "apiKey": "333234567"
            },
            {
                "userId": 7,
                "emailId": "kevin.wei@qdevinc.com",
                "firstName": "abc",
                "lastName": "xyz",
                "nickName": "none",
                "type": "DEVELOPER",
                "apiKey": "111234567"
            },
            {
                "userId": 8,
                "emailId": "dhamji@gmail.com",
                "firstName": "Bab",
                "lastName": "Dham",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "1403790266057"
            },
            {
                "userId": 9,
                "emailId": "info@hemlockhills.ca",
                "firstName": "Jenn",
                "lastName": "Becker",
                "nickName": "JennB",
                "type": "USER",
                "apiKey": "355901361"
            },
            {
                "userId": 10,
                "emailId": "babitadhami1@gmail.com",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 11,
                "emailId": "b.dhami@g.com",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 12,
                "emailId": "dhami.babita@g.com",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 13,
                "emailId": "Francie@abc.com",
                "firstName": "Francie",
                "lastName": "Francie",
                "nickName": "Francie",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 14,
                "emailId": "Sam@abc.com",
                "firstName": "Sam",
                "lastName": "M",
                "nickName": "S",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 15,
                "emailId": "Sid@abc.com",
                "firstName": "Sid",
                "lastName": "B",
                "nickName": "S",
                "type": "USER",
                "apiKey": "22234567"
            },
            {
                "userId": 16,
                "emailId": "tim@outlook.com",
                "firstName": "tim",
                "lastName": "tim",
                "nickName": "tim",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 17,
                "emailId": "racing@gmail.com",
                "firstName": "racing",
                "lastName": "racing",
                "nickName": "Hollywood",
                "type": "USER",
                "apiKey": "222234567"
            }
        ]
    },
    "status": 200,
    "config": {
        "method": "GET",
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "url": "",
        "headers": {
            "Accept": "application/json, text/plain, */*"
        }
    },
    "statusText": "OK"
}]

另一个是

var deviceContactEmail = [
    {
        "id": "1",
        "rawId": "1",
        "displayName": "kandwal",
        "name": {
            "formatted": "kandwal",
            "givenName": "kandwal"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "testuser@g.com",
                "id": "6",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/1/photo",
                "type": "url",
                "id": "1",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "2",
        "rawId": "2",
        "displayName": "xyz",
        "name": {
            "formatted": "xyz ",
            "givenName": "xyz"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "Chris@abc.com",
                "id": "12",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/2/photo",
                "type": "url",
                "id": "7",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "3",
        "rawId": "3",
        "displayName": "cdf",
        "name": {
            "formatted": "cdf",
            "givenName": "cdf"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": null,
        "addresses": [
            {
                "region": "Uk",
                "id": "19",
                "locality": "Dehra Dun",
                "formatted": "dddd",
                "type": "home",
                "pref": false,
                "country": "India"
            }
        ],
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/3/photo",
                "type": "url",
                "id": "14",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "4",
        "rawId": "4",
        "displayName": "abcd",
        "name": {
            "formatted": "scd ",
            "givenName": "scd"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "dhami@gmail.com",
                "id": "26",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/4/photo",
                "type": "url",
                "id": "21",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "5",
        "rawId": "5",
        "displayName": "hiteshbhattcse@gmail.com",
        "name": {
            "formatted": "hiteshbhattcse@gmail.com ",
            "givenName": "hiteshbhattcse@gmail.com"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "hiteshbhattcse@gmail.com",
                "id": "33",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/5/photo",
                "type": "url",
                "id": "28",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "6",
        "rawId": "6",
        "displayName": "hitet@gmail.com",
        "name": {
            "formatted": "hitet@gmail.com ",
            "givenName": "hitet@gmail.com"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "hiteshbhatt@gmail.com",
                "id": "40",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/6/photo",
                "type": "url",
                "id": "35",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "7",
        "rawId": "7",
        "displayName": null,
        "name": {
            "formatted": ""
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "mayank.th088@gmail.com",
                "id": "46",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/7/photo",
                "type": "url",
                "id": "41",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    },
    {
        "id": "8",
        "rawId": "8",
        "displayName": null,
        "name": {
            "formatted": ""
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "gcjoshi83@gmail.com",
                "id": "53",
                "pref": false
            }
        ],
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": "",
        "photos": [
            {
                "value": "content://com.android.contacts/contacts/8/photo",
                "type": "url",
                "id": "48",
                "pref": false
            }
        ],
        "categories": null,
        "urls": null
    }]

我想比较它们的电子邮件 ID 。并希望从 deviceContactEmail 获取 1 个数组内的通用电子邮件 ID 并将其保留在一个数组中。

我尝试过 Angular 。但都没有取得成功。

var wUser = [];
    var nonWUser = [];
angular.forEach(deviceContactEmail, function(phonevalue){               
            console.log(phonevalue);
                 angular.forEach(friendemail, function(value){
                if (phonevalue.emails) {
                if(phonevalue.emails[0].value === value.emailId){
                    console.log(phonevalue.emails[0].value);
                    wUser.push(phonevalue);
                    }else{
                        console.log("------------------------------------------------------------------------");
                        console.log(phonevalue.emails[0].value);
                       nonWUser.push(phonevalue);

                        };
                 }

                })  


            })

最佳答案

Hi Try this I solved to remove the duplicates value in the array check it...


 var wUser = [];
 var nonWUser = [];

   angular.forEach(deviceContactEmail, function(phonevalue){ 
            console.log(phonevalue);
            angular.forEach(friendemail[0].data.userList, function (value) {
                if (phonevalue.emails) {
                    if(phonevalue.emails[0].value === value.emailId){
                        console.log(phonevalue.emails[0].value);
                        wUser.push(phonevalue);
                    }
                    else {
                        console.log("------------------------------------------------------------------------");
                        console.log(phonevalue.emails[0].value);
                        if ($scope.nonWUser.length == 0) {
                            nonWUser.push(phonevalue);                                
                        }
                      var filteredVal=  $scope.nonWUser.filter(function (filterValue) {
                            return filterValue.emails[0].value == phonevalue.emails[0].value;
                      });
                      if (filteredVal.length == 0)
                          nonWUser.push(phonevalue);                          
                    }                     
                }
            });                                
        });  

检查编辑后的文件

关于javascript - 比较 angularJs 中的两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25279183/

相关文章:

angularjs - 如何使用具有 ui-router 状态的 angular 1.5 组件

AngularJS : Extending built-in filters

javascript - 将参数从 angularJs 发送到 php symfony2 api

javascript - 将 angularjs 数据 ID 发送到我的 Node 服务器

php - 如何在没有提交按钮的情况下提交选定的值?

javascript - 当元素未运行时在 C# 中获取 HTML 内部 HTML ="server"

javascript - 单个页面上的 Google Analytics 跟踪

javascript - 创建组件后丢失动画

angularjs - Controller 销毁后 $q notfy 监听器未删除

javascript - AngularJS 中重复范围定义方法