“Nilai duplikat dihapus dalam array objek dalam javascript” Kode Jawaban

cara menghapus objek array duplikat di javascript

const arr = [{id: 1, name: 'one'}, {id: 2, name: 'two'}, {id: 1, name: 'one'}]

const ids = arr.map(o => o.id)
const filtered = arr.filter(({id}, index) => !ids.includes(id, index + 1))

console.log(filtered)
DecodeBuzzing

Hapus objek duplikat dari 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)
 })
Ornate Rock Dragon

cara menghapus objek array duplikat di 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));
Restu Wahyu Saputra

Nilai duplikat dihapus dalam array objek dalam javascript

const arr = [{id: 1, name: 'one', position: 1}, {id: 2, name: 'two', position: 2}, {id: 1, name: 'one', position: 3}]

const ids = arr.map(o => o.id)
const filtered = arr.filter(({id}, index) => !ids.includes(id, index + 1))
console.log(filtered,'filtered')

const uniqueArray = arr.filter((v, i, a) => a.findIndex(t => (t.name === v.name)) == i)
console.log(uniqueArray,'uniqueArray');
$uj@y

Jawaban yang mirip dengan “Nilai duplikat dihapus dalam array objek dalam javascript”

Pertanyaan yang mirip dengan “Nilai duplikat dihapus dalam array objek dalam javascript”

Lebih banyak jawaban terkait untuk “Nilai duplikat dihapus dalam array objek dalam javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya