0

I have two arrays like this :

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": "[email protected]",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 3,
                "emailId": "[email protected]",
                "firstName": "Test_User",
                "lastName": "Account",
                "nickName": "Testi",
                "type": "USER",
                "apiKey": "1403626362113"
            },
            {
                "userId": 4,
                "emailId": "[email protected]",
                "firstName": "dhami",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "DEVELOPER",
                "apiKey": "222234567"
            },
            {
                "userId": 5,
                "emailId": "[email protected]",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "1403709178117"
            },
            {
                "userId": 6,
                "emailId": "[email protected]",
                "firstName": "dhami",
                "lastName": "dhami",
                "nickName": "dhami",
                "type": "DEVELOPER",
                "apiKey": "333234567"
            },
            {
                "userId": 7,
                "emailId": "[email protected]",
                "firstName": "abc",
                "lastName": "xyz",
                "nickName": "none",
                "type": "DEVELOPER",
                "apiKey": "111234567"
            },
            {
                "userId": 8,
                "emailId": "[email protected]",
                "firstName": "Bab",
                "lastName": "Dham",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "1403790266057"
            },
            {
                "userId": 9,
                "emailId": "[email protected]",
                "firstName": "Jenn",
                "lastName": "Becker",
                "nickName": "JennB",
                "type": "USER",
                "apiKey": "355901361"
            },
            {
                "userId": 10,
                "emailId": "[email protected]",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 11,
                "emailId": "[email protected]",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 12,
                "emailId": "[email protected]",
                "firstName": "Babita",
                "lastName": "Dhami",
                "nickName": "bobby",
                "type": "USER",
                "apiKey": "333234567"
            },
            {
                "userId": 13,
                "emailId": "[email protected]",
                "firstName": "Francie",
                "lastName": "Francie",
                "nickName": "Francie",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 14,
                "emailId": "[email protected]",
                "firstName": "Sam",
                "lastName": "M",
                "nickName": "S",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 15,
                "emailId": "[email protected]",
                "firstName": "Sid",
                "lastName": "B",
                "nickName": "S",
                "type": "USER",
                "apiKey": "22234567"
            },
            {
                "userId": 16,
                "emailId": "[email protected]",
                "firstName": "tim",
                "lastName": "tim",
                "nickName": "tim",
                "type": "USER",
                "apiKey": "111234567"
            },
            {
                "userId": 17,
                "emailId": "[email protected]",
                "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"
}]

and another is

var deviceContactEmail = [
    {
        "id": "1",
        "rawId": "1",
        "displayName": "kandwal",
        "name": {
            "formatted": "kandwal",
            "givenName": "kandwal"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "[email protected]",
                "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": "[email protected]",
                "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": "[email protected]",
                "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": "[email protected]",
        "name": {
            "formatted": "[email protected] ",
            "givenName": "[email protected]"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "[email protected]",
                "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": "[email protected]",
        "name": {
            "formatted": "[email protected] ",
            "givenName": "[email protected]"
        },
        "nickname": null,
        "phoneNumbers": null,
        "emails": [
            {
                "type": "other",
                "value": "[email protected]",
                "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": "[email protected]",
                "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": "[email protected]",
                "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
    }]

I want to compare both of them for email id . and wanna get the common email id inside 1 array and rest in one array from deviceContactEmail.

I tried angular. each but not getting success.

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

                        };
                 }

                })  


            })
3
  • Please fix code indentation. Commented Aug 13, 2014 at 6:18
  • its repeating the data Commented Aug 13, 2014 at 6:49
  • hi bro did you check this... Commented Aug 13, 2014 at 9:49

2 Answers 2

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

check the edited file

Sign up to request clarification or add additional context in comments.

2 Comments

deviceContactEmail is getting repeated
hi check out the edited code it does not getting the repeated value... Check it and reply..
0

It seem you are not pointing the inner loop at the right list. friendemail is a list but contains only one item. I assume the emailId you are testing for is buried inside the data which happens to be child of friendemail. Try the code below

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

                    })
                })

2 Comments

Problem is not there as the json was pasted from service thats why was looking like this . It still showing same thing duplicate data
Please which part do you mean that is showing duplicate. Maybe I don't understand your what you intend to achieve. If you explain better I promise to assist. Can you tell me exactly what you expect?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.