javascript return property value from nested array of objects based on condition - Stack Overflow

i have an array of objects, in which each object could have an array of objects inside.var mylist = [{&

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('oute:', result);  

my code is not working at this stage.

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('oute:', result);  

my code is not working at this stage.

Share Improve this question asked May 7, 2018 at 9:31 moaningalwaysmoaningalways 4611 gold badge10 silver badges21 bronze badges 1
  • You must use the find function to find the right course object and then return the name of the found course. The function some only returns a boolean value with the value of the given predicate. – Gilad Bar Commented May 7, 2018 at 9:35
Add a ment  | 

2 Answers 2

Reset to default 3

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745556427a4632848.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信