Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 943
moaningalways Avatar asked May 21 '26 13:05

moaningalways


2 Answers

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);
like image 151
T.J. Crowder Avatar answered May 24 '26 01:05

T.J. Crowder


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)
like image 26
Nenad Vracar Avatar answered May 24 '26 01:05

Nenad Vracar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!