我需要通过匹配ID从两个数组中收集数据,并将该数据推入新数组中。
我试图通过循环来做这些事情,但是它不能准确地推送。 在第一个循环中,我迭代了学生的数组,在第二个循环中,我针对了课程数组的内部循环。
var students = [
{
_id: 'std1',
name: 'Bill Gates',
age: 30
},
{
_id: 'std2',
name: 'Steve Jobs',
age: 30
},
{
_id: 'std3',
name: 'Suzanne Collins',
age: 30
}];
var courses = [
{
name: 'Maths',
std_id: ['std3', 'std1']
},
{
name: 'Computer Science',
std_id: ['std3', 'std2']
},
{
name: 'Commerce',
std_id: ['std1']
},
]
var sorted = [];
for(var i=0; i<students.length; i++){
for(var j=0; j<courses.length; j++){
if(students[i]._id == courses[j].std_id){
sorted.push({'name' : students[i].name, 'studen ID' :students[i]._id, 'courses' :[courses[j].name]})
}
}
}
console.log(sorted);
{
course: ['Maths','Commerce']
name: 'Bill Gates',
studentId: 'std1'
}
答案 0 :(得分:1)
尝试一下:
var students = [
{
_id: 'std1',
name: 'Bill Gates',
age: 30
},
{
_id: 'std2',
name: 'Steve Jobs',
age: 30
},
{
_id: 'std3',
name: 'Suzanne Collins',
age: 30
}];
var courses = [
{
name: 'Maths',
std_id: ['std3', 'std1']
},
{
name: 'Computer Science',
std_id: ['std3', 'std2']
},
{
name: 'Commerce',
std_id: ['std1']
},
]
let sortedArr = [];
for(let i = 0; i < students.length; i++) {
let stCourse = [];
for(let j = 0; j < courses.length; j++) {
if(courses[j].std_id.includes(students[i]._id)) {
stCourse.push(courses[j].name)
}
}
sortedArr.push({name: students[i].name, 'studen ID': students[i]._id, courses: stCourse})
}
console.log(sortedArr)
答案 1 :(得分:1)
var students = [
{
_id: 'std1',
name: 'Bill Gates',
age: 30
},
{
_id: 'std2',
name: 'Steve Jobs',
age: 30
},
{
_id: 'std3',
name: 'Suzanne Collins',
age: 30
}];
var courses = [
{
name: 'Maths',
std_id: ['std3', 'std1']
},
{
name: 'Computer Science',
std_id: ['std3', 'std2']
},
{
name: 'Commerce',
std_id: ['std1']
},
]
const studentWithCourses = students.map(student => {
let stdCourses = courses.filter(course => course.std_id.includes(student._id))
return { ...student, courses: stdCourses.map(c => c.name) }
})
console.log(studentWithCourses)
尝试使用此代码段。我使用了地图和过滤器方法。我为每个学生根据学生证过滤课程。
答案 2 :(得分:0)
您可以这样做
const result = students.map(student => {
const {_id, name} = student;
return {
name: name,
id: _id,
courses: courses.reduce((a, course) => {
course.std_id.includes(_id) && a.push(course.name);
return a;
}, [])
}
});
答案 3 :(得分:0)
您可以使用减速器(Array.reduce
,see MDN)。
该代码段还包含一个map / filter替代项。
const arrays2Combine = getArrays();
// reduce
const combined = arrays2Combine.courses.reduce ( (students, val) => {
students.forEach(student => {
if (val.std_id.includes(student._id)) {
student.courses = (student.courses || []).concat(val.name);
}
})
return students;
}, arrays2Combine.students);
console.log(`**Reduced:\n`, combined);
// Alternative using map/filter
const combined2 = arrays2Combine.students.map( student => {
student.courses = arrays2Combine.courses
.filter(c => c.std_id.includes(student._id))
.map(course => course.name);
return student;
});
setTimeout(() => {
console.clear();
console.log(`**Map/filter alternative\n`, combined2);
},
5000
);
function getArrays() {
return { students:
[
{
_id: 'std1',
name: 'Bill Gates',
age: 30
},
{
_id: 'std2',
name: 'Steve Jobs',
age: 30
},
{
_id: 'std3',
name: 'Suzanne Collins',
age: 30
}
],
courses: [
{
name: 'Maths',
std_id: ['std3', 'std1']
},
{
name: 'Computer Science',
std_id: ['std3', 'std2']
},
{
name: 'Commerce',
std_id: ['std1']
}
]
};
}
答案 4 :(得分:0)
希望对您有帮助
var students = [
{
_id: 'std1',
name: 'Bill Gates',
age: 30
},
{
_id: 'std2',
name: 'Steve Jobs',
age: 30
},
{
_id: 'std3',
name: 'Suzanne Collins',
age: 30
}];
var courses = [
{
name: 'Maths',
std_id: ['std3', 'std1']
},
{
name: 'Computer Science',
std_id: ['std3', 'std2']
},
{
name: 'Commerce',
std_id: ['std1']
},
]
var sorted = [];
let array=[];
for(var i=0; i<students.length; i++){
for(var j=0; j<courses.length; j++){
if(courses[j].std_id.indexOf(students[i]._id)>-1){
array.push(courses[j].name);
}
}
sorted.push({'name' : students[i].name, 'studen ID' :students[i]._id,
'courses' :array})
array=[];
}