@dotabred

Как конкатенировать два массива без дублей?

Есть два массива:

var all_labels_list = [{id: "e:1222", text: "1"},
            {id: "e:1244", text: "11"},
            {id: "e:1237", text: "111"},
            {id: "e:1236", text: "1111"},
            {id: "e:1223", text: "2"},
            {id: "e:1224", text: "3"},
            {id: "e:1225", text: "4"},
            {id: "e:1226", text: "5"},
            {id: "e:1228", text: "6"},
            {id: "e:1229", text: "7"},
            {id: "e:1249", text: "8"},
            {id: "e:1250", text: "9"},
            {id: "e:1220", text: "d"},
            {id: "e:1219", text: "ds"},
            {id: "e:1217", text: "dsd"},
            {id: "e:1227", text: "dsddsdds"},
            {id: "e:1215", text: "dsdsd"},
            {id: "e:1216", text: "dsdsdsd"},
            {id: "e:1232", text: "fdfdfdffdfd"},
            {id: "e:1230", text: "fff"},
            {id: "e:1231", text: "ffff"},
            {id: "e:1221", text: "s"},
            {id: "e:1218", text: "sd"},
            {id: "e:1247", text: "sdsd"},
            {id: "e:1243", text: "sdsdsd"},
            {id: "e:1233", text: "ss"},
            {id: "e:1235", text: "sss"},
            {id: "e:1234", text: "ssss"},
            {id: "e:1212", text: "\u0412\u0435\u0431\x2D\u0441\u0430\u0439\u0442"},
            {id: "e:1242", text: "\u0432\u044B\u0432\u044B\u0432\u044B"},
            {id: "e:1248", text: "\u043D\u043E\u04321"},
            {id: "e:1245", text: "\u043D\u043E\u0432\u0430\u044F\x20\u043C\u0435\u0442\u043A\u0430"},
            {id: "e:1246", text: "\u043D\u043E\u0432\u0430\u044F\x20\u043C\u0435\u0442\u043A\u0430\x20\u0438\u0437\x20\u0441\u043A\u0440\u044B\u0442\u043E\u0433\u043E\x20\u0438\u043D\u043F\u0443\u0442\u0430"},];
            
            
            var selected_labels = [{id: "e:1222", text: "1"},
                {id: "e:1244", text: "11"},
                {id: "e:1237", text: "111"},
                {id: "e:1236", text: "1111"},];


Как объединить два массива объектов сравнивая их по id и есть повторяется, то вывести в консоли?
  • Вопрос задан
  • 540 просмотров
Пригласить эксперта
Ответы на вопрос 2
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
var result = all_labels_list.concat(
  selected_labels.filter(a => !all_labels_list.find(b => a.id == b.id && a.text == b.text))
);
Ответ написан
hzzzzl
@hzzzzl
хмм, а selected_labels не будет входить в all_labels_list? а то как бы если селектед выбирается из всех лейблов, то их сумма будет = all labels

newList = [...all_labels_list]

selected_labels.forEach(l => {
    if(newList(label => label.id === l.id)) {
        console.log('not unique!', l)
    } else {
        newList.push(l)
    }
})
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы