我想通过通用键合并多个数组。我尝试了很多函数,但没有得到我所需要的。所有的功能给我一个结构不好的数组。因此,我需要您的帮助才能获得适当的帮助。我需要打字稿或JavaScript中的函数,这是我的示例
数组1:
"Student":[ {
"id":91,
"name":"john",
"age":22
},
{
"id":11,
"name":"donald",
"age":20},
]
数组2:
"Courses":
[
{"id":91,
"course name":"finance 1",
"course code":"fin12"},
{"id":91,
"course name":"accounting",
"course code":"acc1"}
]
数组3:
"Instructor": [
{
"id":91,
"instructor-name":"justin"
},
{
"id":91,
"instructor-name":"leonardo"
},
{
"id":16,
"instructor-name":"david"
}
]
合并:
{
"id": 91,
"name": "john",
"age": 22,
"Course": [
{
"id": 91,
"course name": "finance 1",
"course code": "fin12"
},
{
"id": 91,
"course name": "accounting",
"course code": "acc1"
}
],
"Instructor": [
{
"id": 91,
"instructor name": "justin"
},
{
"id": 91,
"instructor name": "leonardo"
}
]
}
如果不清楚。请通知我
答案 0 :(得分:0)
您可以映射学生数组并根据id过滤另一个数组,以获取所有课程和讲义器。
const studentArray =[ {
"id":91,
"name":"john",
"age":22
},
{
"id":11,
"name":"donald",
"age":20},
]
const coursesArray =
[
{"id":91,
"course name":"finance 1",
"course code":"fin12"},
{"id":91,
"course name":"accounting",
"course code":"acc1"}
]
const instructorArray = [
{
"id":91,
"instructor-name":"justin"
},
{
"id":91,
"instructor-name":"leonardo"
},
{
"id":16,
"instructor-name":"david"
}
]
const output = studentArray.map(item => {
const id = item.id;
const course = coursesArray.filter(course => id === course.id);
const instructor = instructorArray.filter(i => id === i.id);
return { ...item, course , instructor };
});
console.log(output)