gpt4 book ai didi

javascript - 如何从过滤器的过滤器返回值?

转载 作者:行者123 更新时间:2023-11-30 07:07:02 26 4
gpt4 key购买 nike

我想让所有以数学为主题的学生

var student = [{
name: 'XYZ',
age: 15,
subject: ['Maths', 'Science'],
}, {
name: 'XYZ',
age: 15,
subject: ['Hindi', 'Science'],
}, {
name: 'XYZ',
age: 15,
subject: ['Maths', 'English'],
}];

我已经试过了但是没有用:

student.filter(x=>x.subject.filter(y=>y=='Maths'));

任何帮助将不胜感激

最佳答案

需要结合filter()宽度some()来判断是否有科目Maths

var student = [{
name: 'XYZ',
age: 15,
subject: ['Maths', 'Science'],
},
{
name: 'XYZ',
age: 15,
subject: ['Hindi', 'Science'],
},
{
name: 'XYZ',
age: 15,
subject: ['Maths', 'English'],
}
];

console.log(student.filter(x => x.subject.some(y => y == 'Maths')));

关于javascript - 如何从过滤器的过滤器返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com