1

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?

Pratap A.K
  • 4,337
  • 11
  • 42
  • 79

2 Answers2

2

"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
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

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
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
  • Nice and short. Fails for `findIfIdExists({id:'"'}, 'id', '"')`, but I guess you probably won't have quotation marks in your values. – nnnnnn Apr 04 '17 at 04:23
  • yes I don't have quotation mark in value field and I'm doing all the validations like null, empty, '' before calling this method. – Pratap A.K Apr 04 '17 at 06:17