我有两个对象数组,我想根据条件获取键的值。

我想基于deltaArr中的键id从旧数组中获取googleId值。

因此,当deltaArr的googleId与oldArr的googleId匹配时,我想在该索引处获取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

注意:两个数组的长度根据动态数据而变化。在这种情况下,我不知道idgoogleId的值是什么

最佳答案

您可以将哈希表用于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));

.as-console-wrapper { max-height: 100% !important; top: 0; }

09-19 20:46