0

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

kittu
  • 6,662
  • 21
  • 91
  • 185
  • 1
    Your code only contains one loop. You compare `deltaArr[i]` and `oldArr[i]`, where `i` is the same variable. This will only find matches that occur at the same index in both arrays. – melpomene Dec 31 '16 at 13:24
  • http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Teemu Dec 31 '16 at 13:27
  • @melpomene Exactly that is the problem as length of both arrays vary. How would I loop both arrays? Do I have to use nested for loop? or `array.find()`? – kittu Dec 31 '16 at 13:28
  • 1
    @Satyadev, you can use something like this: https://jsfiddle.net/xw1o4ztL/ but there are more elegant solutions, for sure.... – sinisake Dec 31 '16 at 13:29

2 Answers2

3

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

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));
trincot
  • 317,000
  • 35
  • 244
  • 286
  • What is hashing sir? I am sorry I didn't understand. I am still learning – kittu Dec 31 '16 at 13:49
  • 1
    I use the word hashing here just to say you could *key* the old array elements by their *googleId* values, which means you can then access those elements directly once you have the key. Once of the ways to do that is to create a `Map`, but it also works with plain object key/value pairs. In fact, you can just replace the word *hash* with *key* (or *index*) in my answer. – trincot Dec 31 '16 at 13:53