简体   繁体   中英

How to find object by id in array of objects?

I have this json file:

var data = [{
    "id": 0,
    "parentId": null,
    "name": "Comapny",
    "children": [
        {
            "id": 1235,
            "parentId": 0,
            "name": "Experiences",
            "children": [
                {
                    "id": 3333,
                    "parentId": 154,
                    "name": "Lifestyle",
                    "children": []
                },
                {
                    "id": 319291392,
                    "parentId": 318767104,
                    "name": "Other Experiences",
                    "children": []
                }
            ]
        }
    ]
}];

I need to find object by id. For example if need to find an object with id:319291392, I have to get:

{"id": 319291392,"parentId": 318767104,"name": "Other Experiences","children": []}

How can I do that?

I tried to use this function:

function findId(obj, id) {
    if (obj.id == id) {
        return obj;
    }
    if (obj.children) {
        for (var i = 0; i < obj.children.length; i++) {
            var found = findId(obj.children[i], id);
            if (found) {
                return found;
            }
        }
    }
    return false;
}

But it doesn't work as it's an array of objects.

If your starting point is an array, you want to invert your logic a bit, starting with the array rather than with the object:

function findId(array, id) {
    var i, found, obj;

    for (i = 0; i < array.length; ++i) {
        obj = array[i];
        if (obj.id == id) {
            return obj;
        }
        if (obj.children) {
            found = findId(obj.children, id);
            if (found) {
                return found;
            }
        }
    }
    return false; // <= You might consider null or undefined here
}

Then

var result = findId(data, 319291392);

...finds the object with id 319291392.

Live Example

This should work for you:-

var serachById = function (id,data) {
    for (var i = 0; i < data.length; i++) {
        if(id==data[i].id)
            return data[i];
        if(data[i].children.length>0)
            return serachById(id,data[i].children);
    };
    return null;
}

console.log(serachById(0,data));

Here is another simple solution using object notation. This solution will work even if you decide to get rid of teh array and use object notation later on. so the code will remain the same.

It will also support the case when you have element with no children.

function findId(obj, id) {
    var current, index, reply;

    // Use the object notation instead of index. 
    for (index in obj) {
        current = obj[index];
        if (current.id === id) {
            return current;
        }
        reply = findId(current.children, id);
        if (reply) {
            return reply;
        }

        // If you reached this point nothing was found.
        console.log('No match found');
    }
}

console.log(findId(data, 319291392));

do it so:

for (var obj in arr) {
    if(arr[obj].id== id) {
        console.log(arr[obj]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM