如何从数组对象中获取多个匹配的值?
我的情景是:
var studentData = [
{name:"Ram",htno:1245},
{name:"mohan",htno:1246},
{name:"madhu",htno:1247},
{name:"ranga",htno:1248}
];
var studentsToCompare = ["mohan","ranga"];
Expected output:
var matchedStudentsdata = [
{name:"mohan",htno:1246},
{name:"ranga",htno:1248}
];
由于 Rambabu Bommisetti
答案 0 :(得分:2)
尝试filter()
,如下所示:
var student = [{name:"Ram",htno:1245},{name:"mohan",htno:1246},{name:"madhu",htno:1247},{name:"ranga",htno:1248}]
var matchedvalues = ["mohan","ranga"];
var matchedStudentsdata = student.filter(stu => matchedvalues.includes(stu.name));
console.log(matchedStudentsdata);

答案 1 :(得分:2)
在student
阵列上使用过滤器:
var student = [{name:"Ram",htno:1245},{name:"mohan",htno:1246},{name:"madhu",htno:1247},{name:"ranga",htno:1248}]
var matchedvalues = ["mohan","ranga"];
var matchedStudentsdata = student.filter(function(stu) {
return matchedvalues.indexOf(stu.name) > -1;
})
console.log(matchedStudentsdata)