-2

I want to remove all the duplicate elements from the following array of objects and form a new array.

let arr = [{item: 'abc'},{item: 'def'},{item: 'abc'},{item: 'ghi'}];

expected output:

let newArr =  [{item: 'abc'},{item: 'def'},{item: 'ghi'}];
  • Stack Overflow is a question and answer platform. Do you have a specific question? _"How can I do this"_ isn't a specific programming question. Please don't spam tags. This "question" isn't related to React. – jabaa Mar 30 '23 at 22:34

1 Answers1

0

Filter out items that have a previous matching item

let arr = [{item: 'abc'},{item: 'def'},{item: 'abc'},{item: 'ghi'}];

let newArray = arr.filter((val, index) => !arr.find((val2, index2) => val.item === val2.item && index > index2));

console.log(newArray);
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60