raju
raju

Reputation: 6936

merge 2 array into new array after matching their content javascript

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.

Upvotes: 0

Views: 66

Answers (3)

Nenad Vracar
Nenad Vracar

Reputation: 122067

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)

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386650

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; }

Upvotes: 0

jsadev.net
jsadev.net

Reputation: 2590

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);

Upvotes: 1

Related Questions