Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript find child object in nested arrays

I have a javascript structure like below (nested arrays of objects)

var categoryGroups = [
    {
        Id: 1, Categories: [
            { Id: 1 },
            { Id: 2 }, 
        ]

    },
    {
        Id: 2, Categories: [
            { Id: 100 },
            { Id: 200 },
        ]

    }
]

I want to find a child Category object matching an Id, assuming the Category Id's are all unique.

I've got this below, but was wondering if there is a more concise way of doing it:

var category, categoryGroup, found = false;
for (i = 0; i < categoryGroups.length ; i++) {
    categoryGroup = categoryGroups[i];
    for (j = 0; j < categoryGroup.Categories.length; j++) {
        category = categoryGroup.Categories[j];
        if (category.Id === id) {
            found = true;
            break;
        }
    }
    if (found) break;
}
like image 651
user380689 Avatar asked Mar 12 '14 06:03

user380689


2 Answers

Caveat: This uses a couple of Array.prototype functions that were only added in ECMAScript 5 and thus will not work with older browsers unless you polyfill them.

You can loop over all first-level objects in your array, and then filter the categories based on your condition and collect all matches in an array. Your final result will be the first element in the array of matches (no match found if array is empty).

var matches = [];
var needle = 100; // what to look for

arr.forEach(function(e) {
    matches = matches.concat(e.Categories.filter(function(c) {
        return (c.Id === needle);
    }));
});

console.log(matches[0] || "Not found");

JSFiddle: http://jsfiddle.net/b7ktf/1/

References:

Array.prototype.forEach
Array.prototype.concat
Array.prototype.filter

like image 131
xbonez Avatar answered Nov 19 '22 21:11

xbonez


Using flatMap in ES2019

const category = categoryGroups.flatMap(cg => cg.Categories).find(c => c.Id === categoryId);
like image 18
mlienau Avatar answered Nov 19 '22 21:11

mlienau