-2

Hello I am new in react native and I want to merge data using Object.assign().

I tried with simple arrays and everythings work but my array is more than array in one big array.

My code:

    let o1 = getCartData; // [{productId:56, productQuantity:1}, {productId:4, productQuantity:1}]
    let o2 = resultObject; // product with different quantity {productId:56, productQuantity:5}

    let finalObj = Object.assign([], o1, o2);
    console.log('Final', finalObj); // Output ▼["[","{",""","p","r","o","d","u","c","t","I","d",""",":","5","6",",",""","p","r","o","d","u","c","t","Q","u","a","n","t","i","t","y",""",":","1","}","]"]

I want to get this output:

console.log('Final', finalObj); // Merged [{productId:56, productQuantity:5}, {productId:4, productQuantity:1}]

I tried based on this page

Maher Aldous
  • 894
  • 1
  • 10
  • 14
  • Could you please create a [small demo](https://stackoverflow.com/help/minimal-reproducible-example) for this using [jsfiddle](https://jsfiddle.net/) or [snippet](https://meta.stackoverflow.com/a/358993/1823841) here to show the issue happening. – palaѕн Jun 16 '20 at 14:13

2 Answers2

1

You can map it, Please let me know if this is what you need:

var obj1=[{productId:56, productQuantity:1}, {productId:4, productQuantity:1}];
var obj2={productId:56, productQuantity:5};
var result = obj1.map(({productId, productQuantity})=>({ productId, productQuantity: obj2[`productId`]==productId ? obj2.productQuantity : productQuantity}));
 
 console.log(result);

Or you can make use of reduce method to group data:

var obj1=[{productId:56, productQuantity:1}, {productId:4, productQuantity:1}];
var obj2={productId:56, productQuantity:5};
var result = [...obj1, obj2].reduce((acc, {productId, productQuantity})=>{
    acc[productId] = acc[productId] || {productId, productQuantity};
    acc[productId].productQuantity = productQuantity;
    return acc;
},{});

console.log(Object.values(result));
gorak
  • 5,233
  • 1
  • 7
  • 19
  • You saved my life and my time that I already spent. I was working on this almost a week but don't know what to do because the `asyncStorage.mergeItem` didn't work so I had to merge manually. Just one more thing can I fix the same results if I hade like this array ________ `[ pay:[ { one: 3 }, { two: 5 } ] products: [ { "productId": 4, "productQuantity": 1 }, { "productId": 56, "productQuantity": 5 } ], other:[ {...}, {...} ] ]` – Maher Aldous Jun 16 '20 at 19:27
1

You can do this using map and find:

arr1.map(arrObj => arr2.find(arr2Obj => arr2Obj.productId === arrObj.productId) || arrObj);

What this code does is:

  • Inside the array of objects, it iterates through each object, in here each object is called arrObj.
  • For each object called arrObj we assign an operation using the arrow operator (=>), that operation is going to return a value.
  • Now we use array.find(()=>{}), which will seek on the second array and will search for every item inside the second array and we put a conditional for it, if the conditional is true, it will return the value of the found item, if not it will return null.
  • We use a (||) operator that will trigger when the item is not found, we return the object itself, as there is no other object found with the same Id.

Notice that this code works for comparing two arrays, in your example you are trying to compare an array with an object, it can be easily solved if your o2 is equal to [resultObject] (Inside the [brackets] to turn it into an array).

Got this solution from here: Replacing objects in array

Leandro
  • 36
  • 6