Javascript 比较angularJs中的两个数组

Javascript 比较angularJs中的两个数组,javascript,angularjs,Javascript,Angularjs,我有两个这样的阵列: var friendemail = [ { "data": { "status": "OK", "message": "User list fetched successfully", "userList": [ { "userId": 1, "emailId": "abc.com", "first

我有两个这样的阵列:

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,其余的在一个数组中

我试过了。每一次都是如此,但都没有成功

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

                        };
                 }

                })  


            })

检查编辑过的文件

似乎您没有将内部循环指向右侧列表。friendemail是一个列表,但只包含一项。我假设您正在测试的emailId被隐藏在数据中,而这些数据恰好是friendemail的子数据。请尝试下面的代码

angular.forEach(deviceContactEmail, function(phonevalue) {
                    console.log("................  ",phonevalue, "     ", friendemail[0].data.userList.length);
                    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);
                                nonWUser.push(phonevalue);
                            }
                            ;
                        }

                    })
                })

请修复代码缩进。它正在重复数据Hi bro您是否检查此…deviceContactEmail正在重复Hi签出已编辑的代码它没有获得重复值。。。检查并回答..问题不存在,因为json是从服务粘贴的,这就是为什么看起来像这样。它仍然显示相同的东西重复数据请问您指的是显示重复的部分。也许我不明白你的意图。如果你解释得更好,我保证会帮你。你能确切地告诉我你期望什么吗?
angular.forEach(deviceContactEmail, function(phonevalue) {
                    console.log("................  ",phonevalue, "     ", friendemail[0].data.userList.length);
                    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);
                                nonWUser.push(phonevalue);
                            }
                            ;
                        }

                    })
                })