0

I want to remove duplicates from an array of objects can anyone help with this to remove duplicates in the array of objects. I have an array of array of objects that I want to remove duplicates and I want to use

const firstArray = [
  {
    first: "01",
    data: [
      { id: "01", name: "test1" },
      { id: "02", name: "test2" },
      { id: "03", name: "test3" },
      { id: "05", name: "test1" },
      { id: "06", name: "test2" },
      { id: "07", name: "test1" }
    ]
  },
  {
    first: "02",
    data: [
      { id: "01", name: "test2" },
      { id: "02", name: "test1" },
      { id: "03", name: "test3" },
      { id: "05", name: "test2" },
      { id: "06", name: "test2" },
      { id: "07", name: "test1" }
    ]
  },
  {
    first: "03",
    data: [
      { id: "01", name: "test3" },
      { id: "02", name: "test2" },
      { id: "03", name: "test3" },
      { id: "04", name: "test2" },
      { id: "05", name: "test3" },
      { id: "07", name: "test1" }
    ]
  }
];

this is the sample result code i am expecting :

const firstArray = [
  {
    first: "01",
    data: [
      { id: "01", name: "test1" },
      { id: "02", name: "test2" },
      { id: "03", name: "test3" },
    ]
  },
  {
    first: "02",
    data: [
      { id: "01", name: "test1" },
      { id: "02", name: "test2" },
      { id: "03", name: "test3" },
    ]
  },
  {
    first: "03",
    data: [
      { id: "05", name: "test1" },
      { id: "06", name: "test2" },
      { id: "07", name: "test3" }
    ]
  }
];```
10
  • 2
    May you share an attempt of your own, and the expected output? Commented Jan 4, 2022 at 17:30
  • 1
    Duplicates at what level? Show your expected result. Commented Jan 4, 2022 at 17:31
  • And the expected output is.......? Commented Jan 4, 2022 at 17:33
  • I have given sample expected code Commented Jan 4, 2022 at 17:36
  • As soon as the OP does specify the definition of duplicate (here most probably any equal name value of a data item within each separate data array), the OP him/herself might get more clear about an own approach. The most important information for any approach of cause was whether one wants another array as result or one wants to mutate the original(ly provided) array reference (here firstArray). Commented Jan 4, 2022 at 17:52

2 Answers 2

1

this way..

const firstArray = 
    [ { first: '01'
      , data: 
        [ { id: '01', name: 'test1' } 
        , { id: '02', name: 'test2' } 
        , { id: '03', name: 'test3' } 
        , { id: '05', name: 'test1' } 
        , { id: '06', name: 'test2' } 
        , { id: '07', name: 'test1' } 
      ] } 
    , { first: '02'
      , data: 
        [ { id: '01', name: 'test2' } 
        , { id: '02', name: 'test1' } 
        , { id: '03', name: 'test3' } 
        , { id: '05', name: 'test2' } 
        , { id: '06', name: 'test2' } 
        , { id: '07', name: 'test1' } 
      ] } 
    , { first: '03'
      , data: 
        [ { id: '01', name: 'test3' } 
        , { id: '02', name: 'test2' } 
        , { id: '03', name: 'test3' } 
        , { id: '04', name: 'test2' } 
        , { id: '05', name: 'test3' } 
        , { id: '07', name: 'test1' } 
    ] } ] 
  
firstArray.forEach(({data})=> 
  {
  for (let i=data.length;--i>0;) 
  if (data.findIndex(({name})=>name===data[i].name) < i)
    data.splice(i,1)
  })


console.log( JSON.stringify( firstArray,0,2))
.as-console-wrapper {max-height: 100%!important;top:0 }

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

3 Comments

hi @Mister, i want to enter the value to filter , can i use like this data.findIndex(({name})=>name=== "test1",
everything is doable but i don't understand what i'm asking: filter elements on a key or eliminate all duplicates?
thank you for helping
0

const firstArray = [
    {
      first: "01",
      data: [
        { id: "01", name: "test1" },
        { id: "02", name: "test2" },
        { id: "03", name: "test3" },
        { id: "05", name: "test1" },
        { id: "06", name: "test2" },
        { id: "07", name: "test1" }
      ]
    },
    {
      first: "02",
      data: [
        { id: "01", name: "test2" },
        { id: "02", name: "test1" },
        { id: "03", name: "test3" },
        { id: "05", name: "test2" },
        { id: "06", name: "test2" },
        { id: "07", name: "test1" }
      ]
    },
    {
      first: "03",
      data: [
        { id: "01", name: "test3" },
        { id: "02", name: "test2" },
        { id: "03", name: "test3" },
        { id: "04", name: "test2" },
        { id: "05", name: "test3" },
        { id: "07", name: "test1" }
      ]
    }
  ];


let filterValues = firstArray.map((item) => {
  let data = item.data.filter(
    (value, index, data) =>
      data.findIndex((item) => item.name === value.name) === index
  );
  return { ...{ first: item.first }, data };
});

console.log(filterValues);

Comments

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.