Reputation: 7018
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
Upvotes: 0
Views: 93
Reputation: 386654
You could use a hash table for googleId
and check the old
array if a known googleId
is found, then add the id
to the result array.
The hash table is an object with googleId
as key and true
as value.
{
"2bf235bd8a846814": true
}
This is necessary for the check of the old
array. If the hash is '2bf235bd8a846814'
, the hash table returns true
, otherwise it returns undefied
, which is a falsy value for the if
condition.
function getId(delta, old) {
var googleId = Object.create(null),
result = [];
delta.forEach(function (a) {
googleId[a.googleId] = true;
});
console.log(googleId);
old.forEach(function(a) {
if (googleId[a.googleId]) {
result.push(a.id);
}
});
return result;
}
var deltaArray = [{ "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": "" }],
oldArray = [{ "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": "" }];
console.log(getId(deltaArray, oldArray));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3
Reputation: 350335
You could hash the old array by its google_id values (using a Map), and feed that as the context for a .map()
call on the delta array:
function collect(delta, old) {
return delta.map(function (d) {
return this.get(d.googleId).id
}, new Map(old.map ( o => [o.googleId, o] )));
}
var 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": "" }];
var 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": "" }];
console.log(collect(deltaArr, oldArr));
Upvotes: 2