Answer the question
In order to leave comments, you need to log in
How to find objects and add them to arrays of objects without duplication?
There is an array of objects (tasks):
[
{
"id": "0",
"id_cource": "2",
"name": "Задача",
"teacher": [
{"id": "6"}],
"student": [
{"id":"2"},
{"id": "0"}
],
},
{
"id": "1",
"id_cource": "2",
"name": "Задача",
"teacher": [
{"id": "6"},
{"id": "4"},
{"id": "0"}],
"student": [
{"id":"0"}
],
},
...etc
]
(2) [{...}, {...}]
. const teacherTaskCource = [];
contains (3) [ {"id": "6"},{"id": "4"},{"id": "0"}]
. const studentTaskCource = [];
which is (2) [ {"id": "2"},{"id": "0"}].
tasksCourse.filter((i) => teacherTaskCource.push(i.teachers))
Answer the question
In order to leave comments, you need to log in
Well, like this
// объект который будет хранить айдишники учителей
const freq = {}
// финальный массив
const res = [];
for (const el of tasks.teacher) {
// для каждого учителя смотрим, уникальный ли он
// если учитель есть в объекте freq, значит не уникальный, едем дальше
if (el.id in freq) continue;
// в противном случае записываем айди учителя в объект и пушим объект учителя в результирующий массив
freq[el.id] = 1;
res.push(el);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question