1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/songjinyang-everyday

 / Детали:

### Задача 1 Дан массив: ```javascript let arr = [ {id:1,name:"s"}, {id:2,name:'j'}, {id:1,name:'y'} ] ``` Необходимо удалить объекты, у...

Предстоит сделать
Владелец
Создано  
27.04.2025

которых значение 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)

Комментарий (0)

GitLife Service Account Задача создана

Вход Перед тем как оставить комментарий

Статус
Ответственный
Контрольная точка
Pull Requests
Связанные запросы на слияние могут быть закрыты после их объединения
Ветки
Дата начала   -   Крайний срок
-
Закрепить/Открепить
Приоритет
Участники(1)
1
https://api.gitlife.ru/oschina-mirror/songjinyang-everyday.git
git@api.gitlife.ru:oschina-mirror/songjinyang-everyday.git
oschina-mirror
songjinyang-everyday
songjinyang-everyday