简体   繁体   English

javascript根据条件从对象的嵌套数组返回属性值

[英]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.

相关问题 JavaScript:基于n级嵌套属性值过滤掉数组对象的最优解 - JavaScript : Optimal solution to filtered out the objects from an array based on the n-level nested property value Javascript从属性值匹配的对象数组返回数组 - Javascript return array from array of objects where property value matches 根据属性值过滤嵌套的对象数组 - Filter nested array of objects based on a property value 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 根据 Javascript 中的属性值从数组的 object 中获取唯一对象 - Getting unique objects from object of array based on property value in Javascript 根据值从对象数组中选择一个属性:Javascript - Select a property from an array of objects based on a value : Javascript 如何根据条件检查对象数组中的重复属性 javascript - How to check the duplicates property in array of objects based on condition javascript 使用 javascript 根据属性从数组中删除对象 - Remove objects from array based on their property with javascript Map 通过对象的深层嵌套数组并根据条件更新值 - Map through the deep nested array of objects and update the value based on condition 如何根据属性值过滤数组并返回对象数组 - How to filter an array based on property value and return array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM