1

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,}]
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
thechoosenone
  • 129
  • 2
  • 8
  • 16

3 Answers3

2

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 } ]
Matteo Ragni
  • 2,837
  • 1
  • 20
  • 34
Soudipta Dutta
  • 1,353
  • 1
  • 12
  • 7
  • Welcome to Stack Overflow. Code-only answers are discouraged because they do not explain how they resolve the issue in the question. Consider updating your answer to explain what this does and how it addresses the problem - this will help not just the OP but others with similar issues. Please review [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) – FluffyKitten Sep 19 '17 at 10:39
1

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);
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
  • 3
    Perhaps `Object.assign(a, b)` would be closer to the desired solution? (Or any other way of merging two objects.) Of course that'd mutate `a1`, so another array, combined, could be created, followed by (on match) `combined.push(Object.assign({}, a, b));` – Dawson Toth Oct 03 '16 at 22:55
  • @Lucas Costa , thank you for helping hand, but the results is not one i was trying to get, if you see the combined array of example first object will have 5 key value pairs which is made of combination of a1 and a2 since flight number matched and second object will be just 3 key value pairs because no flight number was matching. – thechoosenone Oct 03 '16 at 22:58
  • Thanks for suggestion @DawsonToth, I'm aggre with you and Redu asnwered based in `Object.assign`. – BrTkCa Oct 03 '16 at 23:08
  • Note that this still mutates `a1` because `result = a1` is an assignment by reference to the original array. `result = a1.slice(0)` would leave `a1` unchanged. – Dawson Toth Oct 04 '16 at 00:48
  • Thanks @DawsonToth – BrTkCa Oct 04 '16 at 12:58
0

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);
Redu
  • 25,060
  • 6
  • 56
  • 76