How can I remove duplicates in an array of object?

user10863293

I have an array which looks like this :

var array = 
[
    {
        key : { id : 1 , pack : "pack 1"},
        values : [
            {
                item : { id : 1 , name : "item1"},
                itemP : {id : 2 , name : "itemP12"}
            },
            {
                item : { id : 4 , name : "item4"},
                itemP : {id : 2 , name : "itemP12"}
            },
        ]
    }
]

I want to remove duplicate itemP so with a function it will look like this :

var array =
[
    {
        key : { id : 1 , pack : "pack 1"},
        values : [
            {
                item : { id : 1 , name : "item1"},
                itemP : {id : 2 , name : "itemP12"}
            },
            {
                item : { id : 4 , name : "item4"},
                itemP : null
            },
        ]
    }
]

When I try I always have errors. It is possible to do this?

Update

I try to do this :

  console.log(array.map(pack => 
    pack.values.map((item) => {
      var test =  JSON.stringify(item)
      var set = new Set(test)

      return Array.from(set).map((item)=> JSON.parse(item))
      }
    )

  ))

Unexpected end of JSON input

I also try something will filter but it doesn't work:

  console.log(this.package.map(pack => pack.values.filter(
    (value, index , array) => array.itemP.indexOf(value) === index
  )))
Washington Guedes

Instead of mapping every key property, I suggest cloning the whole structure and setting the object value as null in the cloned one, avoiding unintentionally mutating the original structure.

function nullifyDupes(array) {
  const clone = JSON.parse(JSON.stringify(array));
  const seen = {};

  clone.forEach(pack => {
    pack.values.forEach(items => {
      for (const item in items) {
        const id = items[item].id;

        if (seen[id]) items[item] = null;
        else seen[id] = true;
      }
    });
  });
  
  return clone;
}

const originalArray = [{
  key : { id : 1 , pack : "pack 1"},
  values : [{
    item : { id : 1 , name : "item1"},
    itemP : {id : 2 , name : "itemP12"}
  },
  {
    item : { id : 4 , name : "item4"},
    itemP : {id : 2 , name : "itemP12"}
  }]
}];

const mutatedArray = nullifyDupes(originalArray);
console.log(mutatedArray);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I remove a £ symbol from an array object and save it?

Remove duplicates in an object array Javascript

How do I remove duplicates from a C# array?

How can I remove object from array, with Lodash?

How can I remove duplicates from a list in Scala with pattern matching?

How can I remove duplicates before calling create in Dynamics CRM?

How do I remove duplicates from an AutoHotkey array?

How to remove duplicates from an object array using spread operator

How can i remove nested or parent object from array in javascript

How can I remove duplicates sorted by traits?

How to remove duplicates from an array object property value?

How can I iterate through xml and remove the duplicates? C#

Remove duplicates from an object array

How can I remove an object from an array if I know the value of one field of the object?

How can I remove duplicates in an array without using `uniq`?

How can I remove object in one array from another array?

How can I improve the time complexity of the following program " Remove duplicates from an array"?

How do I remove duplicates from an array

How can I remove and count duplicates in an array of objects? - Javascript

Given an Array of strings how do I Remove Duplicates?

How can I remove all array elements that have duplicates, leaving only single values?

How to remove duplicates in an array?

How can I remove duplicates from array without using another array?

How can I sort words and remove duplicates of variable in R?

How can i subtract object values having similar keys in an array of object? (not to be confused with removing duplicates)

How can i remove an object from an array state in Redux/react

How to get the sum of a key of consecutive array object and remove the duplicates?

How can I remove a property from an object in an array?

How can i remove Specific item from Formdata object array?