0

I have 2 arrays, and I want a new array based upon condition that the content of these 2 arrays matched

arr1 = [{
    package_id: 'aabbccdd',
    level: 2
  },
  {
    package_id: 'xycd21',
    level: 3
  }
]

arr2 = [{
    package_id: 'aabbccdd',
    level: 1
  },
  {
    package_id: 'zcb21',
    level: 5
  }]

mergedArray = [{
    package_id: 'aabbccdd',
    arr1Level: 2,
    arr2Level: 1
  },
  {
    package_id: 'xycd21',
    arr1Level: 3,
    arr2Level: 0
  },
  {
    package_id: 'zcb21',
    arr1Level: 0,
    arr2Level: 5
  }]

So if package_id is to be checked in both arrays. And if found in either array, new array pushed one element where level from both array is mentioned against package_id.

I just could not figure out the logic to do that. If that can be done by lodash kindly tell me.

Rajesh
  • 24,354
  • 5
  • 48
  • 79
raju
  • 6,448
  • 24
  • 80
  • 163
  • Have you tried anything? Even an algo would be great if you share – Rajesh Apr 07 '17 at 08:14
  • 2
    You're expected to try to **write the code yourself**.If you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Stack Overflow isn't a coding service. – George Apr 07 '17 at 08:15
  • Possible Duplicate: http://stackoverflow.com/questions/33850412/merge-javascript-objects-in-array-with-same-key. Its not exact duplicate so not voting to close but should give you what you need to solve. – Rajesh Apr 07 '17 at 08:17

3 Answers3

1

You can easily solve this using loops like i did here:

var arr1 = [{
    package_id: 'aabbccdd',
    level: 2
  },
  {
    package_id: 'xycd21',
    level: 3
  }
];

var arr2 = [{
    package_id: 'aabbccdd',
    level: 1
  },
  {
    package_id: 'zcb21',
    level: 5
  }
];
  
var mergedArr = [];
var tempObj = {};

for(var i = 0; i < arr1.length; i++){
  tempObj.package_id = arr1[i].package_id;
  tempObj.arr1Level = arr1[i].level;
  tempObj.arr2Level = 0;
  for(var k = 0; k < arr2.length; k++){
    if(arr1[i].package_id === arr2[k].package_id){
      tempObj.arr2Level = arr2[k].level;
    }
  }
  mergedArr.push(tempObj);
  tempObj = {};
}

for(i = 0; i < arr2.length; i++){
  var isNew = true;
  for(k = 0; k < mergedArr.length; k++){
    if(arr2[i].package_id === mergedArr[k].package_id){
      isNew = false;
    }
  }
  if(isNew){
    tempObj.package_id = arr2[i].package_id;
    tempObj.arr2Level = arr2[i].level;
    tempObj.arr1Level = 0;
    mergedArr.push(tempObj);
    tempObj = {};
  }
}

console.log(mergedArr);
jsadev.net
  • 2,800
  • 1
  • 16
  • 28
  • Dear, Thanks a lot, it works. And I also agree to other people view that there should be no spoon feeding. But believe me I am working on an Ionic2 project and wrote almost 3500 lines of codes, with and without help. I just could not figure out the logic for this code. thanks a lot again. – raju Apr 07 '17 at 09:10
1

You can first add both arrays to one array and then use reduce() and forEach() to create new array of objects. Also you can use another object to group elements by package_id

var arr1 = [{ package_id: 'aabbccdd', level: 2 }, { package_id: 'xycd21', level: 3 }];
var arr2 = [{ package_id: 'aabbccdd', level: 1 }, { package_id: 'zcb21', level: 5 }];

var o = {}
var arrays = [arr1, arr2]
var keys = Array.from(Array(arrays.length), (e, i) => ({['arr' + (i + 1) + 'Level']: 0}))

var result = arrays.reduce(function(r, a, i) {
  a.forEach(function(e) {
    if (!o[e.package_id]) {
      o[e.package_id] = Object.assign({}, {package_id: e.package_id}, ...keys)
      r.push(o[e.package_id]);
    }
    o[e.package_id]['arr' + (i + 1) + 'Level'] = e.level;
  })
  return r;
}, [])

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

You could use a structure approach with dynamic keys for different arrays and use the keys as help for generating default values.

var arr1 = [{ package_id: 'aabbccdd', level: 2 }, { package_id: 'xycd21', level: 3 }],
    arr2 = [{ package_id: 'aabbccdd', level: 1 }, { package_id: 'zcb21', level: 5 }],
    keys = [{ level: 'arr1level' }, { level: 'arr2level' }],
    result = [arr1, arr2].reduce(function (hash) {
        return function (r, a, i) {
            a.forEach(function (b) {
                if (!hash[b.package_id]) {
                    hash[b.package_id] = { package_id: b.package_id },
                    r.push(hash[b.package_id]);
                }
                keys.forEach(function (key) {
                    Object.keys(key).forEach(function (k) {
                        hash[b.package_id][key[k]] = 0;
                    });
                });
                Object.keys(b).forEach(function (k) {
                    var key = keys[i][k] || k;
                    hash[b.package_id][key] = b[k];
                });
            });
            return r;
        };
    }(Object.create(null)), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392