比较angularJs中的两个数组

时间:2014-08-13 06:11:12

标签: 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。并希望获得1个数组中的公共电子邮件ID,并从deviceContactEmail中停留在一个数组中。

我尝试了角度。每一个都没有成功。

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

                        };
                 }

                })  


            })

2 个答案:

答案 0 :(得分:1)

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

检查编辑过的文件

答案 1 :(得分:0)

似乎你没有将内循环指向正确的列表。 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);
                            }
                            ;
                        }

                    })
                })