我有两个对象数组,我想根据条件得到key的值。
我想根据deltaArr中的键id
从旧数组中获取googleId
个值。
因此,当 googleId of deltaArr 与 googleId of oldArr 匹配时,我希望在该索引处获得oldArr的id
并存储在数组中。
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": ""
}
]
如果您看到两个阵列中都存在"googleId": "2bf235bd8a846814"
。
我尝试了上述内容,但是给出了undefined
注意:两个数组的长度因动态数据而异。在这种情况下,我不知道id
或googleId
答案 0 :(得分:3)
您可以为googleId
使用哈希表,如果找到已知的old
,则检查googleId
数组,然后将id
添加到结果数组中。
哈希表是一个对象,googleId
为关键字,true
为值。
{
"2bf235bd8a846814": true
}
这对于检查old
数组是必要的。如果散列为'2bf235bd8a846814'
,则散列表返回true
,否则返回undefied
,这是if
条件的假值。
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));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 1 :(得分:2)
您可以通过 google_id 值(使用Map)对旧数组进行哈希处理,并将其作为delta数组上.map()
调用的上下文提供:
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));
&#13;