如何以更现代的 ES6 方式实现嵌套在 for 中的 for?
How to implement a for nested in a for in a more modern ES6 way?
有人让我解决这个问题:Return 只有那些 属性“enrollmentId”在另一个数字数组中的对象:所以我想到了这个,而且它有效,但是,我在 for 中使用 for,我想知道解决此类问题的最佳方法是什么。请检查一下
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4]
const finalResult = [];
for(i=0; i< companions.length; i++){
let alumno = companions[i];
for(j=0; j< participants.length; j++){
let participante = participants[j];
if(alumno.enrollmentId == participante){
finalResult.push(alumno);
}
}
}
console.log(finalResult)
根据被迭代对象的enrollmentId
是否包含在participants
中过滤原始数组。
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7, 2, 4]
const finalResult = companions.filter(obj => participants.includes(obj.enrollmentId));
console.log(finalResult)
我会使用 Array.filter to omit invalid rows, and Array.includes 进行有效性测试
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4];
const finalResult = companions.filter(companion => {
return participants.includes(companion.enrollmentId);
});
console.log(finalResult);
如果您想要 ES6 方式,请使用 code_monk 代码,但不使用 code_monk 代码,都会尊重(我无法在他的代码中添加注释,因为我是堆栈溢出的新手并且没有足够的声誉。)但是箭头函数 ( => ) 不需要关键字 return 也不需要函数 budy {},因为它隐含了一个值将在 return 之后编辑=>
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4];
const finalResult = companions.filter(companion => participants.includes(companion.enrollmentId));
console.log(finalResult);
有人让我解决这个问题:Return 只有那些 属性“enrollmentId”在另一个数字数组中的对象:所以我想到了这个,而且它有效,但是,我在 for 中使用 for,我想知道解决此类问题的最佳方法是什么。请检查一下
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4]
const finalResult = [];
for(i=0; i< companions.length; i++){
let alumno = companions[i];
for(j=0; j< participants.length; j++){
let participante = participants[j];
if(alumno.enrollmentId == participante){
finalResult.push(alumno);
}
}
}
console.log(finalResult)
根据被迭代对象的enrollmentId
是否包含在participants
中过滤原始数组。
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7, 2, 4]
const finalResult = companions.filter(obj => participants.includes(obj.enrollmentId));
console.log(finalResult)
我会使用 Array.filter to omit invalid rows, and Array.includes 进行有效性测试
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4];
const finalResult = companions.filter(companion => {
return participants.includes(companion.enrollmentId);
});
console.log(finalResult);
如果您想要 ES6 方式,请使用 code_monk 代码,但不使用 code_monk 代码,都会尊重(我无法在他的代码中添加注释,因为我是堆栈溢出的新手并且没有足够的声誉。)但是箭头函数 ( => ) 不需要关键字 return 也不需要函数 budy {},因为它隐含了一个值将在 return 之后编辑=>
const companions = [
{name:"camila", enrollmentId:1},
{name:"oscar", enrollmentId:2},
{name:"rupertina", enrollmentId:3}
];
const participants = [7,2,4];
const finalResult = companions.filter(companion => participants.includes(companion.enrollmentId));
console.log(finalResult);