1

I have 2 arrays of objects:

array1 = [{"id":1,"cost":200,"qty":56},{"id":2,"cost":100,"qty":16}];
array2 = [{"id":1,"cost":200,"desc":"a good one"},{"id":2,"cost":100,"desc":"a bad one"},{"id":3,"cost":50,"desc":"an okay one"}];

I want to merge them so it looks like this:

[{"id":1,"cost":200,"qty":56,"desc":"a good one"},{"id":2,"cost":100,"qty":16,"desc":"a bad one"}];

Please notice the new array has properties from both arrays but it left out the object that was not present in the first one.

I tried this:

var mergethem = function() {
     var array1 = [{"id":1,"cost":200,"qty":56},{"id":2,"cost":100,"qty":16}];
     var array2 = [{"id":1,"cost":200,"desc":"a good one"},{"id":2,"cost":100,"desc":"a bad one"},{"id":3,"cost":50,"desc":"an okay one"}];
    
     var newarray= array2.filter(i => array1.map(a=> { if(a.id == i.id) i.qty = a.qty; return i; }));
     
     return newarray.filter(i=> { if(i.qty) return i; });
    }
    
    console.log(mergethem());

this seems to work sometimes and some times it doesn't depending on the environment. I can't pinpoint what the problem is so I would like to ask for alternatives to try out.

4

2 Answers 2

2

You could get references of the objects of the second array and map the first while adding properties of the second array, if exist.

const
    array1 = [{ id: 1, cost: 200, qty: 56 }, { id: 2, cost: 100, qty: 16 }],
    array2 = [{ id: 1, cost: 200, desc: "a good one" }, { id: 2, cost: 100, desc: "a bad one" }, { id: 3, cost: 50, desc: "an okay one" }],
    references2 = Object.fromEntries(array2.map(o => [o.id, o])),
    result = array1.map(o => ({ ...o, ...references2[o.id] }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

You already posted comment that this solution is more robust, awesome! The only thing worth mentioning - that we need somehow be sure that 'o.id' is in our array of references - otherwise we would get 'undefined' I believe?
Sorry but I get this error: Object.fromEntries I am on node.js
@ByteMaster, undefined spreads in objects into as empty object.
@CainNuke, mabe you take a new edition of node. does spreading ... works?
probably but I dont wanna risk breaking my whole application by upgrading node.
|
1

const array1 = [
  {"id":1,"cost":200,"qty":56},
  {"id":2,"cost":100,"qty":16}
];

const array2 = [
  {"id":1,"cost":200,"desc":"a good one"},
  {"id":2,"cost":100,"desc":"a bad one"},
  {"id":3,"cost":50,"desc":"an okay one"}
];

const result = array1.reduce((previousValue, currentValue) => {
  const needObj = array2.find(o => o.id === currentValue.id) ?? {};
  previousValue.push({...currentValue, ...needObj});
  return previousValue;
}, []);

console.log(result);

6 Comments

this works only if the items are at the same index for merging. even op is usning a find function and checks id of both arrays.
Cool, cool - it takes me a while to understand why and how this works. Thanks for such an interesting approach! Basically trick here is that secondly 'spread-ed' object would be iterated over all keys - and all of the keys would be written into object A! And because we have same value for the 'id' key - we would get expanded object!
@Nina Scholz , yes, or maybe the objects go sequentially? )) But you are right, I have edited the answer according to your comment. Thank you!
Sorry I get this error: Unexpected token ? (the one before {};)
I might as well just take that part out like this: const needObj = array2.find(o => o.id === currentValue.id); seems to work anyway
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.