Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中按id查找对象_Javascript_Json_Tree - Fatal编程技术网

在Javascript中按id查找对象

在Javascript中按id查找对象,javascript,json,tree,Javascript,Json,Tree,我有一棵树: { "id": 0, "parentId": null, "name": "Comapny", "children": [ { "id": 1235, "parentId": 0, "name": "Experiences", "children": [ { "id":

我有一棵树:

{
    "id": 0,
    "parentId": null,
    "name": "Comapny",
    "children": [
        {
            "id": 1235,
            "parentId": 0,
            "name": "Experiences",
            "children": [
                {
                    "id": 3333,
                    "parentId": 154,
                    "name": "Lifestyle",
                    "children": [],
                    "merchants": [
                        {
                            "id": 348,
                            "name": "Experience Mad"

                        },
                        {
                            "id": 30,
                            "name": "Virgin Experience Days"
                        }
                    ]
                },
                {
                    "id": 319291392,
                    "parentId": 318767104,
                    "name": "Other Experiences",
                    "children": [],
                    "merchants": [
                        {
                            "id": 353,
                            "name": "Tasterlab"
                        },
                        {
                            "id": 19,
                            "name": "Activity Superstore"
                        }
                    ]
                }
            ],
            "merchants": [
                {
                    "id": 35715,
                    "name": "Red Letter Days"
                },
                {
                    "id": 85,
                    "name": "The Hut"
                }
            ]
        }
    ]
}
我需要按id查找对象。例如,如果需要查找id为353的对象,我必须获得:

{"id": 353,"name": "Tasterlab"} 
如果我需要找到id为1235的对象,我必须得到:
{“id”:1235,“name”:“Experiences”}

我尝试了很多次,但考虑到树的复杂性,我很难做到我想要的。

函数findId(obj,id){
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;
            }
        }
    }
    if (obj.merchants) {
        for (i = 0; i < obj.merchants.length; i++) {
            found = findId(obj.merchants[i], id);
            if (found) {
                return found;
            }
        }
    }
    return false;
}
if(obj.id==id){ 返回obj; } if(对象儿童){ for(变量i=0;i
代码取自:

根据键、值或键和值匹配返回对象数组

返回与某个键匹配的值数组

返回与特定值匹配的键数组


这只是一个简单的递归函数。在循环中使用循环。不要忘记
“商家”
数组;-)谢谢Barmar,但是我得到了“TypeError:obj.children未定义”如果一个对象可能没有子对象或商品,那么您需要一个
If
语句来测试属性是否首先存在。
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(obj);
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(obj);
            }
        }
    }
    return objects;
}
function getValues(obj, key) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getValues(obj[i], key));
        } else if (i == key) {
            objects.push(obj[i]);
        }
    }
    return objects;
}
function getKeys(obj, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getKeys(obj[i], val));
        } else if (obj[i] == val) {
            objects.push(i);
        }
    }
    return objects;
}