How to delete duplicate values of objects inside array?

Zkk

I found several references about how to delete duplicate values in object like this, this, this... In all these examples (and in others that I've found) are for simple objects, but my case is more "complex".

I have an array where each index has your values and an object inside. I create a simple example for explanation:

var data = [
  {
    "name": "Kyle",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      },
      {
        "id": 3,
        "name": "name3"
      },
      {
        "id": 2,
        "name": "name2"
      }
      {
        "id": 3,
        "name": "name3"
      }
    ]
  },
  {
    "name": "Liza",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "John",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "Melissa",
    "item": [
      {
        "id": 2,
        "name": "name2"
      }
    ]
  }
]

How you can see, inside my item objects I've duplicate values... I would like to remove this duplicate values inside my objects.

One way to do this (which I thought) was do it making a copy of my list and using a forEach/map/filter but I don't know how to do it.

I tried this, but not works:

let dataCopy = data;

dataCopy.forEach(dataItem => {
  listPrev.forEach(dataPrev => {

    //??

    //and or...
    if(dataPrev.item.id == dataItem.item.id){          
      //??

    }
  })
})

I'm trying to get this kind of return:

var data = [
  {
    "name": "Kyle",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 2,
        "name": "name2"
      },
      {
        "id": 3,
        "name": "name3"
      },
    ]
  },
  {
    "name": "Liza",
    "item": [
      {
        "id": 1,
        "name": "name1"
      }
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "John",
    "item": [
      {
        "id": 1,
        "name": "name1"
      },
      {
        "id": 4,
        "name": "name4"
      },
      {
        "id": 2,
        "name": "name2"
      }
    ]
  },
  {
    "name": "Melissa",
    "item": [
      {
        "id": 2,
        "name": "name2"
      }
    ]
  }
]

How I can do it? Someone can help me?

tymeJV

Simple forEach and filter will do:

data.forEach(d => {
    d.item = d.item.filter((item, index, self) => {
        return index === self.findIndex(z => z.id == item.id)
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining objects with duplicate values inside an array

how to delete duplicate records from array of objects?

Delete duplicate in array of objects

How can I delete the duplicate objects inside Array when I fetch newsApi

How to duplicate values inside a numpy array?

How to remove duplicate values inside an array in MONGODB?

How to remove both duplicate values in an array of objects

How to merge duplicate values in array of objects

How to get duplicate values in an array of objects

how to delete null values in array of json objects

How to delete duplicate values?

removing duplicate objects inside of array

How to get all values of objects inside array

How to compare values in an array, that is inside an array of objects, with a property of the objects?

How to get an array of the values of all properties of objects inside an array of objects?

Grouping duplicate objects into an array of values

How to check, remove and replace duplicate values inside an Array List?

How to remove duplicate values inside a list array in MongoDB?

How to remove duplicate data values from an array of objects in javascript?

How can I check if the array of objects have duplicate property values?

How to get array of objects with no duplicate values by adding 1 using javascript

How to delete an object from state when it is inside an array of objects

How to get values from an array, inside another array, with stdClass Objects?

PHP how to delete array values inside foreach loop?

How to extract values of a property inside an array of objects- JS

How print values in arrays in array, inside multiple objects

How to access key/pair values within objects inside of an array in Ansible?

How to store separately the repeated values inside array of objects

How to get min values among properties inside an array of objects?

TOP Ranking

HotTag

Archive