-1

I have two arrays of objects, and want to find the corresponding objects to modify the first array, like so:

const arr1 = [
  { order: 1, material: "b", qty: 4 },
  { order: 2, material: "f", qty: 7 },
  { order: 3, material: "a", qty: 8 },
];

const arr2 = [
  { order: 2, material: "f", sku: 45 },
  { order: 3, material: "a", sku: 32 },
  { order: 1, material: "b", sku: 65 },
];

Desired output:

arr1 = [
  { order: 1, material: "b", qty: 4, sku: 65 },
  { order: 2, material: "f", qty: 7, sku: 45 },
  { order: 3, material: "a", qty: 8, sku: 32 },
];

I'm guessing there are many ways to do this and I'd be grateful for any solution. I'm out of my depth on this one and don't know where to start other than multiple loops, which I can't get to work properly.

veronica_s
  • 19
  • 2
  • 1
    Look at this answer https://stackoverflow.com/questions/63172320/update-add-remove-object-in-array-if-exist-in-other-array-by-id/63172414?noredirect=1#comment111709437_63172414 – Kevin Grosgojat Jul 31 '20 at 14:26
  • 4
    This has been answered a million times, search for "Merge arrays by keys in javascript" – feedy Jul 31 '20 at 14:26
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – satanTime Jul 31 '20 at 15:02
  • I realize I was unclear in my question. The values are not unique and there are instances in arr2 that read for example order: 2, material: "b". Thank you kindly for the responses, however – veronica_s Jul 31 '20 at 16:51

3 Answers3

1

You first need to save the sku of each object in a Map identified by the order and material. And then, add the property to arr1 elements in another loop as follows:

const arr1 = [
  { order: 1, material: "b", qty: 4 },
  { order: 2, material: "f", qty: 7 },
  { order: 3, material: "a", qty: 8 },
];

const arr2 = [
  { order: 2, material: "f", sku: 45 },
  { order: 3, material: "a", sku: 32 },
  { order: 1, material: "b", sku: 65 },
];

let map = {};
for(let i = 0; i < arr2.length; i++)
     map[ arr2[i]['order']+arr2[i]['material'] ] = arr2[i]['sku'];

for(let i = 0; i < arr1.length; i++)
     if(map[ arr1[i]['order']+arr1[i]['material'] ])
          arr1[i]['sku'] = map[ arr1[i]['order']+arr1[i]['material'] ];
          
console.log(arr1);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

This will work if order property is unique in both elements.

const arr1 = [
  { order: 1, material: "b", qty: 4 },
  { order: 2, material: "f", qty: 7 },
  { order: 3, material: "a", qty: 8 },
];

const arr2 = [
  { order: 2, material: "f", sku: 45 },
  { order: 3, material: "a", sku: 32 },
  { order: 1, material: "b", sku: 65 },
];
/*
Desired output:

arr1 = [
  { order: 1, material: "b", qty: 4, sku: 65 },
  { order: 2, material: "f", qty: 7, sku: 45 },
  { order: 3, material: "a", qty: 8, sku: 32 },
];

*/

function combine(arr1, arr2) {
  const result = []
  // make sure arr1 order property is always sorted
  arr1.sort((a, b) => {
    if(a.order > b.order) return 1
    else if(a.order < b.order) return -1
    return 0
  })
  
  arr1.forEach(el => {
    const index = arr2.findIndex(el2 => el2.order === el.order)
    
    if(index !== -1) {
      el.sku = arr2[index].sku
      result.push(el)
    }
  })
  return result
}

console.log(combine(arr1, arr2))
Grampet
  • 177
  • 1
  • 1
  • 8
  • 1
    The properties were not unique, this got lost in my simplification of the arrays when posting the question. Thank you so much for the very quick response though. I have upvoted, but don't have enough points for it to show – veronica_s Jul 31 '20 at 14:46
-1
for (let i = 0; i < arr1.length; i++){
    for (let j = 0; j < arr2.lentgh; j++){
        if (arr1[i].order == arr2[j].order){
            arr1[i].sku = arr2[j].sku;
        }
    }
}
3174N
  • 188
  • 2
  • 14