繁体   English   中英

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

[英]Get value of key based on key of another object

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

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

所以,当deltaArr的googleIdoldArr的googleId比赛,我想id在数组的索引和存储在oldArr的。

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"两个数组中都存在"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; } 

您可以通过旧数组的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)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM