Answers for "js remove duplicates from array of objects"

7

remove duplicate objects from array javascript

const addresses = [...]; // Some array I got from async call

const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
 .map(id => {
   return addresses.find(a => a.id === id)
 })
Posted by: Guest on January-02-2020
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
1

how to remove duplicate array object in javascript

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

const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(obj);
Posted by: Guest on April-22-2020
1

how to remove duplicate array object in javascript

let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];

// REMOVE DUPLICATE FOR ARRAY LITERAL
const arrOne = new Set(days);
console.log(arrOne);

const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
console.log(arrTwo);


// REMOVE DUPLICATE FOR ARRAY OBJECT
const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
console.log(arrObjOne);

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

how to remove duplicate array object in javascript

var arrayWithDuplicates = [
    {"type":"LICENSE", "licenseNum": "12345", state:"NV"},
    {"type":"LICENSE", "licenseNum": "A7846", state:"CA"},
    {"type":"LICENSE", "licenseNum": "12345", state:"OR"},
    {"type":"LICENSE", "licenseNum": "10849", state:"CA"},
    {"type":"LICENSE", "licenseNum": "B7037", state:"WA"},
    {"type":"LICENSE", "licenseNum": "12345", state:"NM"}
];

function removeDuplicates(originalArray, prop) {
     var newArray = [];
     var lookupObject  = {};

     for(var i in originalArray) {
        lookupObject[originalArray[i][prop]] = originalArray[i];
     }

     for(i in lookupObject) {
         newArray.push(lookupObject[i]);
     }
      return newArray;
 }

var uniqueArray = removeDuplicates(arrayWithDuplicates, "licenseNum");
console.log("uniqueArray is: " + JSON.stringify(uniqueArray));
Posted by: Guest on April-22-2020
-1

remove duplicates from array of objects javascript

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

const uniqueArray = arr.filter((v,i,a)=>a.findIndex(t=>(t.name===v.name))===i)
console.log(uniqueArray);
Posted by: Guest on June-21-2021

Code answers related to "js remove duplicates from array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language