简体   繁体   中英

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.

Probably simplest in this case just to assign directly to 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:

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.

 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM