javascript - 根据另一个对象的键获取键的值

标签 javascript arrays

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

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

因此,当 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": ""
    }
]

旧地址:

[
  {
    "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"。 我尝试了上面的方法,但它给出了未定义

注意:两个数组的长度根据动态数据而变化。在这种情况下,我不知道 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; }

关于javascript - 根据另一个对象的键获取键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408597/

相关文章:

javascript - 面对php、mysql中的Make Like Like Button使用ajax调用

JavaScript 数组 : Not functioning past 500 elements

javascript - ExtendScript 从搜索的 Comp 结果中获取索引号

javascript - 通过子数组处理数组

c - 如何在 C 中使用 void 函数更改数组?

javascript - Array.reduce 和递归

javascript - 通过值对对象中的值进行排序 - js

c++ - 从 CArray 指针访问值

javascript - HMAC/Javascript - 在哪里存储 secret ?

选择选项错误时Javascript显示隐藏的div