Слияние кода завершено, страница обновится автоматически
которых значение id
совпадает с id
других объектов в массиве. Решение: javascript let arr = [ {id:1,name:"s"}, {id:2,name:'j'}, {id:1,name:'y'} ]; let uniqueArr = arr.filter((item, index, array) => { return array.findIndex(i => i.id === item.id) === index; }); console.log(uniqueArr);
Этот код использует метод filter
, чтобы оставить только уникальные объекты по значению id
.
// Iterate over this array, using the id of each element as a property of a new object. If the property is missing, add the element to a new array.
// Use this value as an object property to avoid adding elements with the same id in subsequent iterations.
let obj = {}
let res = []
for (let item of arr) {
if (!obj[item.id]) {
res.push(item)
obj[item.id] = item.id
}
}
console.log(res)