thechoosenone
thechoosenone

Reputation: 145

How to combine two array objects if a key value matches in both arrays

I am trying to combine two array objects with different array objects based on flight key.
If the value is matched in a2, I want to merge it with a1 and create new array.
All data in a1 must be there and matched flight details must be added from a2 to a1.

Please help me to solve this.

a1 = [{'flight':'AF1223','oring':'CDF', 'Dest':'HNG'},{'flight':'XG23','oring':'HYD', 'Dest':'MMZ'},{'flight':'PK145','oring':'XYZ', 'Dest':'PEK'}]

a2 = [{'price':230,'avail':20,'flight':'AF1223'}, {'price':430,'avail':30,'flight':'DF43'},{'price':430,'avail':30,'flight':'XG23'} ]

combine array = [{'flight':'AF1223','oring':'CDF', 'Dest':'HNG','price':230,'avail':20},{'flight':'XG23','oring':'HYD', 'Dest':'MMZ'},{'flight':'PK145','oring':'XYZ', 'Dest':'PEK','price':430,'avail':30,}]

Upvotes: 1

Views: 3767

Answers (3)

Soudipta Dutta
Soudipta Dutta

Reputation: 2122

I am giving you a complete solution.

a = [
  {'id':'A','oring':'CDF', 'Dest':'HNG'},
  {'id':'B','oring':'HYD', 'Dest':'MMZ'},
  {'id':'C','oring':'XYZ', 'Dest':'PEK'}];

b = [
  {'id':'A','price':230,'avail':20,}, 
  {'id':'D','price':430,'avail':30},
  {'id':'B','price':430,'avail':30} ];

// put the object D into array a
for(var i=0;i<b.length;i++) {
  var idb = b[i].id;
  var idnotfound = true;
  a.forEach(function(rowofa) {
    var ida = rowofa.id;
    if(ida == idb) {
      idnotfound = false;
    }
  });
  if(idnotfound){
    a.push(b[i]);
  }
}//for
//console.log('aa=>>',a);
//.......................................

//Match ids and then put all in array a

for(var i= 0;i<a.length;i++) {
  var ida = a[i].id;
  for(var j=0;j<b.length;j++) {
    var idb = b[j].id;
    if(ida == idb) {
      a[i].avail = b[j].avail;
      a[i].price = b[j].price;
    }//if
  } //for2
}//for1
console.log('aa=>',a);

The output will be

aa=> [ { id: 'A', oring: 'CDF', Dest: 'HNG', avail: 20, price: 230 },
       { id: 'B', oring: 'HYD', Dest: 'MMZ', avail: 30, price: 430 },
       { id: 'C', oring: 'XYZ', Dest: 'PEK' },
       { id: 'D', price: 430, avail: 30 } ]

Upvotes: 2

BrTkCa
BrTkCa

Reputation: 4783

You can iterate over two arrays and if match push object from array2 ito array1, something like..

var a1 = [{'flight':'AF1223','oring':'CDF', 'Dest':'HNG'},{'flight':'XG23','oring':'HYD', 'Dest':'MMZ'},{'flight':'PK145','oring':'XYZ', 'Dest':'PEK'}];

var a2 = [{'price':230,'avail':20,'flight':'AF1223'}, {'price':430,'avail':30,'flight':'DF43'},{'price':430,'avail':30,'flight':'XG23'} ];

var result = a1.slice(0);

for (var i = 0 ; i < result.length ; i++){
  for (var j = 0; j < a2.length ; j++){
    if (result[i].flight == a2[j].flight){
      result[i].price = a2[j].price;
      result[i].avail = a2[j].avail;
    }
  };  
};
console.log(result);

Upvotes: 1

Redu
Redu

Reputation: 26161

To match your desired output you can do as follows;

var a1 = [{'flight':'AF1223','oring':'CDF', 'Dest':'HNG'},{'flight':'XG23','oring':'HYD', 'Dest':'MMZ'},{'flight':'PK145','oring':'XYZ', 'Dest':'PEK'}],
    a2 = [{'price':230,'avail':20,'flight':'AF1223'}, {'price':430,'avail':30,'flight':'DF43'},{'price':430,'avail':30,'flight':'XG23'}],
result = a2.reduce((p,c) => (p[c.flight] && (p[c.flight] = Object.assign(p[c.flight],c)),p), a1.reduce((f,s) => (f[s.flight] = s,f),{}));
console.log(result);

Upvotes: 0

Related Questions