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

moaningalways

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.

T.J. Crowder

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);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Group array of objects by string property value in JavaScript?

分類Dev

Sorting an array based on property value of another array of objects

分類Dev

Search nested array of objects and return full parents as results in JavaScript

分類Dev

Get average value from array consisting of objects based on objects fields

分類Dev

Fetch unique values from array of objects based on dynamically passed property

分類Dev

Get min/max value from array based on condition

分類Dev

merge objects in same array based on common value in Javascript

分類Dev

Regroup array of objects based on nested array

分類Dev

Destruct nested array of objects javascript

分類Dev

Filter javascript array based on condition

分類Dev

Convert an array of objects to a hash based on a property of the object

分類Dev

Javascript create array from existing split on property value

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly

分類Dev

Update nested object property based on the value of another property - JS

分類Dev

JAVA: How do I merge objects from an arrayList based on a property value?

分類Dev

How to return a value from a async function in array.map in javascript?

分類Dev

Adding new value to String array based on condition

分類Dev

order or sort array of objects based on a key value

分類Dev

How to merge Array of Objects based on the same value?

分類Dev

How to extract a property from array of objects and slice it?

分類Dev

Return Nested Json from database as an array with Laravel

分類Dev

Filter javascript objects array based on a multivalue object

分類Dev

Search nested array of objects and return the full path to the object

分類Dev

Remove object based on a key from array of objects

分類Dev

Lumen/Laravel - Method to extract specific property from objects in an array of objects?

分類Dev

Extract a value from an object in array matching a condition

分類Dev

Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition

分類Dev

How to get the combination of array values from nested arrays in an array of objects

Related 関連記事

  1. 1

    Group array of objects by string property value in JavaScript?

  2. 2

    Sorting an array based on property value of another array of objects

  3. 3

    Search nested array of objects and return full parents as results in JavaScript

  4. 4

    Get average value from array consisting of objects based on objects fields

  5. 5

    Fetch unique values from array of objects based on dynamically passed property

  6. 6

    Get min/max value from array based on condition

  7. 7

    merge objects in same array based on common value in Javascript

  8. 8

    Regroup array of objects based on nested array

  9. 9

    Destruct nested array of objects javascript

  10. 10

    Filter javascript array based on condition

  11. 11

    Convert an array of objects to a hash based on a property of the object

  12. 12

    Javascript create array from existing split on property value

  13. 13

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  14. 14

    Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly

  15. 15

    Update nested object property based on the value of another property - JS

  16. 16

    JAVA: How do I merge objects from an arrayList based on a property value?

  17. 17

    How to return a value from a async function in array.map in javascript?

  18. 18

    Adding new value to String array based on condition

  19. 19

    order or sort array of objects based on a key value

  20. 20

    How to merge Array of Objects based on the same value?

  21. 21

    How to extract a property from array of objects and slice it?

  22. 22

    Return Nested Json from database as an array with Laravel

  23. 23

    Filter javascript objects array based on a multivalue object

  24. 24

    Search nested array of objects and return the full path to the object

  25. 25

    Remove object based on a key from array of objects

  26. 26

    Lumen/Laravel - Method to extract specific property from objects in an array of objects?

  27. 27

    Extract a value from an object in array matching a condition

  28. 28

    Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition

  29. 29

    How to get the combination of array values from nested arrays in an array of objects

ホットタグ

アーカイブ