[英]javascript return property value from nested array of objects based on condition
i have an array of objects, in which each object could have an array of objects inside. 我有一个对象数组,其中每个对象都可以在其中包含对象数组。
var mylist = [
{
"email" : null,
"school" : "schoolA",
"courses": [
{
"name" : 'ABC',
"type" : "chemistry"
},
{
"name" : 'XYZ',
"type": "math"
}
]
},
{
"email" : null,
"school": "schoolB"
}
];
i want to return course name if one of the course type is chemistry. 如果课程类型之一是化学,我想返回课程名称。 The course types are unique and even if they are some duplicates, we return the first one.
课程类型是唯一的,即使它们是重复的,我们也会返回第一个。
var result = mylist.some(function (el) {
el.courses && el.courses.some(function(u) {
if (u.type === 'chemistry') {
return u.name;
};
})
});
console.log('outcome:', result);
my code is not working at this stage. 我的代码在此阶段无法正常工作。
The some
callback should return a truthy or falsy value, which tells some
whether to keep going ( true
= stop), and some
returns a boolean, not a callback return value. some
回调应该返回一个true或falsy值,它告诉some
是否继续( true
= stop),而some
返回一个布尔值,而不是回调返回值。
Probably simplest in this case just to assign directly to result
: 在这种情况下,将
result
直接分配可能是最简单的:
var result;
mylist.some(function(el) {
return (el.courses || []).some(function(course) {
if (course.type === "chemistry") {
result = course.name;
return true;
}
return false;
});
});
Live Example: 现场示例:
var mylist = [ { "email" : null, "school" : "schoolA", "courses": [ { "name" : 'ABC', "type" : "chemistry" }, { "name" : 'XYZ', "type": "math" } ] }, { "email" : null, "school": "schoolB" } ]; var result; mylist.some(function(el) { return (el.courses || []).some(function(course) { if (course.type === "chemistry") { result = course.name; return true; } return false; }); }); console.log(result);
I stuck to ES5 syntax since you didn't use any ES2015+ in your question, but in ES2015+, simplest probably to use nested for-of
loops: 我坚持使用ES5语法,因为您在问题中未使用任何ES2015 +,但在ES2015 +中,最简单的方法可能是使用嵌套的
for-of
循环:
let result;
outer: for (const el of mylist) {
for (const course of el.courses || []) {
if (course.type === "chemistry") {
result = course.name;
break outer;
}
}
}
Live Example: 现场示例:
const mylist = [ { "email" : null, "school" : "schoolA", "courses": [ { "name" : 'ABC', "type" : "chemistry" }, { "name" : 'XYZ', "type": "math" } ] }, { "email" : null, "school": "schoolB" } ]; let result; outer: for (const el of mylist) { for (const course of el.courses || []) { if (course.type === "chemistry") { result = course.name; break outer; } } } console.log(result);
You could use reduce()
method to iterate through each object in array and then find()
method to find if some course matches type. 您可以使用
reduce()
方法遍历数组中的每个对象,然后使用find()
方法查找某些课程是否与类型匹配。
var mylist = [{"email":null,"school":"schoolA","courses":[{"name":"ABC","type":"chemistry"},{"name":"XYZ","type":"math"}]},{"email":null,"school":"schoolB"}] const course = mylist.reduce((r, {courses}) => { if (courses && !r) { const course = courses.find(({type}) => type == 'chemistry'); if (course) r = course.name; } return r; }, null) console.log(course)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.