I have two arrays of objects and I want to get value of key based on a condition.
I want to get id
values from old array based on key googleId
from deltaArr.
So when googleId of deltaArr matches with googleId of oldArr, I want to get id
of oldArr at that index and store in array.
function getPrimaryKeys(deltaArr, oldArr){
console.log('deltaArr........ ', JSON.stringify(deltaArr, null, 2));
console.log('oldArr........ ', JSON.stringify(oldArr, null, 2));
var primaryKeys = [];
for(var i = 0; i< deltaArr.length; i++){
if(deltaArr[i].hasOwnProperty('googleId') && deltaArr[i].googleId == oldArr[i].googleId){
primaryKeys.push(oldArr[i].id);
}
}
return primaryKeys;
}
deltaArr:
[
{
"contact": {
"address": {
"home": "",
"office": ""
},
"email": {
"home": "",
"other": "",
"work": ""
},
"im": {
"aim": "",
"icq": "",
"skype": ""
},
"phone": {
"cell": "+91-1234-567-891",
"home": "",
"work": "",
"e164": "+911234567891"
}
},
"googleId": "2bf235bd8a846814",
"createdDate": "2016-12-31T13:03:09.203Z",
"name": "Test1",
"profileData": ""
}
]
oldArr:
[
{
"contact": {
"address": {
"home": "",
"office": ""
},
"email": {
"home": "",
"other": "",
"work": ""
},
"im": {
"aim": "",
"icq": "",
"skype": ""
},
"phone": {
"cell": "+91-1234-567-896",
"e164": "+911234567896",
"home": "",
"work": ""
}
},
"createdDate": "2016-12-31T12:59:08.959Z",
"googleId": "3e98af288ff825f7",
"id": "2e4009de-4bce-4f02-b33c-415ad688f1c2",
"name": "Test6",
"profileData": ""
},
{
"contact": {
"address": {
"home": "",
"office": ""
},
"email": {
"home": "",
"other": "",
"work": ""
},
"im": {
"aim": "",
"icq": "",
"skype": ""
},
"phone": {
"cell": "+91-1234-567-890",
"e164": "+911234567890",
"home": "",
"work": ""
}
},
"createdDate": "2016-12-31T12:59:08.952Z",
"googleId": "2bf235bd8a846814",
"id": "411b2507-64a1-46d6-812b-8216446676e3",
"name": "Test0",
"profileData": ""
},
{
"contact": {
"address": {
"home": "",
"office": ""
},
"email": {
"home": "",
"other": "",
"work": ""
},
"im": {
"aim": "",
"icq": "",
"skype": ""
},
"phone": {
"cell": "+91-1234-567-895",
"e164": "+911234567895",
"home": "",
"work": ""
}
},
"createdDate": "2016-12-31T12:59:08.951Z",
"googleId": "20735d9e8df44423",
"id": "46f579cb-dbda-49f1-8eb6-df621692e023",
"name": "Test5",
"profileData": ""
}
]
If you see "googleId": "2bf235bd8a846814"
is present in both the arrays.
I tried the above but its giving undefined
Note: Length of both arrays vary based on dynamic data. In this case, I would not know what is value of id
or googleId