1

I have the following variable:

var allProducts = [
    {"code": 1,"name": "productA", "category": ["fruits"],...},
    {"code": 1,"name": "productA", "category": ["vegetables"],...},
    {"code": 2,"name": "productB", "category": ["meat"],...},
    ...
]

So the only difference between the two repeated array of objects is the category; where in this example code: 1 is once mentioned with category: ["fruits"] and another time with category: ["vegetables"]. Now I want to remove the duplicate but before doing so; I would like to save all the categories of productA into one category: ["fruits", "vegetables"] so the final variable would look like this:

var allProductsCleaned = [ 
    {"code": 1,"name": "productA", "category": ["fruits", "vegetables"],...},
    {"code": 2,"name": "productB", "category": ["meat"]...},
    ...
]
3
  • 2
    I think you put wrong object, key type in the variable. It should be like this? var allProducts = [ {"code": 1,"name": "productA", "category": ["fruits"],...},... Commented Apr 28, 2020 at 6:57
  • Does this answer your question? merging duplicates in javascript array Commented Apr 28, 2020 at 10:50
  • Edited question as objects were not formatted correctly. Commented Apr 30, 2020 at 13:17

1 Answer 1

2

Here's an example:

  • Create an object with reduce:
    • save each object into the aggregated Object to test if "code" already added
    • if already present then merge the arrays
  • transform the object back to an array with Object.values()

const allProducts = [
    {"code": 1,"name": "productA", "category": ["fruits"]},
    {"code": 1,"name": "productA", "category": ["vegetables"]},
    {"code": 2,"name": "productB", "category": ["meat"]},
    {"code": 2,"name": "productB", "category": ["fish"]},
    {"code": 2,"name": "productB", "category": ["fish"]}
]

const output = Object.values(allProducts.reduce((aggObj, item) => {  
  if (aggObj[item.code]){
    //item already exists so merge the category arrays:
    const newArr = [...new Set([...aggObj[item.code].category, ...item.category])]
    aggObj[item.code].category = newArr;
  }else{
    //doesn't already exist:
    aggObj[item.code] = item;
  }  
  return aggObj
}, {}));

console.log(output);

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

3 Comments

Perfect! This is exactly what I was looking for. Thank you Alex! Quick question if the product with code 1 here; have the same category for both as ["fruits"]; with your above code it will merge it and output the category as ` ["fruits", "fruits" ]. Right? How can I prevent that when if the category array` contains the word fruit to not merge them?
Great, See my updated answer - we can use the ES6 new Set([iterable]) to ensure unique entries (there are other ways to do it, like using arr.inlcudes(item)). If my answer solved your problem please consider to mark as the accepted answer and up-vote :)
You are awesome! upvoted and accepted as answer! thank you

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.