簡體   English   中英

在Javascript中按ID查找對象

[英]Find object by id in Javascript

我有這棵樹:

{
    "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"}

我嘗試了很多次,但是鑒於樹的復雜性,我很難做到自己想要的。

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

代碼來自: http : //techslides.com/how-to-parse-and-search-json-in-javascript/

根據鍵,值或鍵和值的匹配返回對象數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM