Answers for "remove duplicate values from object in javascript"

1

remove duplicates in array object javascript

//check one attribute
let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];

person = person.filter((item, index, self) =>
  index === self.findIndex((t) => (
    t.name === item.name
  ))
)

//check two attributes
let person = [{place: "uno", name: "john"}, {place: "duno", name: "jane"}, {place: "duno" ,name: "imelda"}, {place: "uno" ,name: "john"}];

person = person.filter((item, index, self) =>
  index === self.findIndex((t) => (
    t.place === item.place && t.name === item.name
  ))
)
Posted by: Guest on September-08-2021
0

how to remove duplicate array object in javascript

let person = [
{name: "john"}, 
{name: "jane"}, 
{name: "imelda"}, 
{name: "john"},
{name: "jane"}
];

const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(data);
Posted by: Guest on April-22-2020
0

Removing duplicates in an Array of Objects

// BEST ANSWER - LINEAR TIME SOLUTION

const seen = new Set();
const arr = [
  { id: 1, name: "test1" },
  { id: 2, name: "test2" },
  { id: 2, name: "test3" },
  { id: 3, name: "test4" },
  { id: 4, name: "test5" },
  { id: 5, name: "test6" },
  { id: 5, name: "test7" },
  { id: 6, name: "test8" }
];

const filteredArr = arr.filter(el => {
  const duplicate = seen.has(el.id);
  seen.add(el.id);
  return !duplicate;
});
Posted by: Guest on September-10-2020

Code answers related to "remove duplicate values from object in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language