简体   繁体   中英

Effective ways to check if the given ID exist inside nested objects in Javascript

What is the best way to check if the given ID exist inside nested objects in JavaScript.

Object

campusElement = {
        "id": "C1",
        "name": "camp",
        "buildings": [{
            "id": "B1",
            "name": "B-name",
            "floors": [{
                "id": "F1",
                "name": "F-name",
                "rooms": [{
                    "id": "R1",
                    "name": "R-name"
                }]
            }]
        }]
    }

currently I'm looping through entire objects and doing

component.ts

isIdExists(elementID: string) {
    var isIdUnique = false;
    if (campusElement.id === elementID) {
        isIdUnique = true;
    } else {
        for (const building of campusElement.buildings) {
            if (building.id === elementID) {
                isIdUnique = true;
                break;
            } else {
                for (const floor of building.floors) {
                    if (floor.id === elementID) {
                        isIdUnique = true;
                        break;
                    } else {
                        for (const room of floor.rooms) {
                            if (room.id === elementID) {
                                isIdUnique = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    return isIdUnique;
}

is there a better way to do this?

"Best" is subjective, but you could write a generic recursive function that looks for a property with a specified name and value, and if it doesn't find it at the top level iterate over all properties to check for any that are arrays and if so check each of the array elements recursively:

 function propertyExists(obj, propName, propValue) { if (obj[propName] === propValue) return true for (var k in obj) { if (Array.isArray(obj[k]) && obj[k].some(function(v) { return typeof v === "object" && propertyExists(v, propName, propValue) })) { return true } } return false } campusElement = { "id": "C1", "name": "camp", "buildings": [{ "id": "B1", "name": "B-name", "floors": [{ "id": "F1", "name": "F-name", "rooms": [{ "id": "R1", "name": "R-name" }] }] }] } console.log(propertyExists(campusElement, "id", "C1")) // true console.log(propertyExists(campusElement, "id", "R1")) // true console.log(propertyExists(campusElement, "name", "R-name")) // true console.log(propertyExists(campusElement, "id", "no matching value")) // false console.log(propertyExists(campusElement, "no matching prop name", "C1")) // false console.log(propertyExists(campusElement, "rooms", "X1")) // false 

now I am doing this this using java script "includes"

findIfIdExists(object, key, value){
   return JSON.stringify(object).includes('"'+key+'":"'+value+'"');
}

and

console.log(findIfIdExists(campusElement, 'id', 'C1')); //true

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