search for a particular key in a nested object in nodejs

I have a nested object which looks like this :

let obj = {
_id:{}
person:{
    $search:{fname:true}
    _id:{},
    fname:{}
},
code:{},
vnvEmpName:{}

}

I have to search for a $search keyword in this and get the key which is inside it that is fname in this case, it can contain multiple keys as well and I have to retrieve all of it.

I tried something like this :

function findById(obj, id) {                             

var result;
for (var p in obj) {
    if (obj.id === id) {
        return obj;
    } else {
        if (typeof obj[p] === 'object') {
            result = findById(obj[p], id);
            if (result) {
                return result;
            }
        }
    }
}
return result;

}

If the object is in this way :

let obj = {
_id: {},
person: {
    $search: {
        lname: true
    },
    _id: {},
    fname: {},
    something:{
        $search: {
            fname: true
        },
    }
},
code: {},
$search: {
    mname: true
},
vnvEmpName: {}

}
I want to retrieve all the attributes inside the $search of every block.

but I don’t know how to get the keys inside a particular key as I am so new to the javascript.

2 thoughts on “search for a particular key in a nested object in nodejs”

  1. Need to recursively search through the object

    let obj = {
      _id: {},
      person: {
        $search: {
          fname: true
        },
        _id: {},
        fname: {}
      },
      code: {},
      vnvEmpName: {}
    }
    
    function findById(obj, id) {
      var result = "";
      // iterate the object using for..in
      for (var keys in obj) {
        // check if the object has any property by that name 
        if (obj.hasOwnProperty(keys) && typeof obj[keys] === 'object') {
          // if the key is not undefined get it's value
          if (obj[keys][id] !== undefined) {
            result = (obj[keys][id])
          } else {
            // else again call the same function using the new obj value
            findById(obj[keys], id)
          }
        }
    
      }
      return result;
    }
    console.log(findById(obj, 'fname'))
    Reply
  2. You can use the following function:

    const objectifier = function (splits, create, context) {
        let result = context;
    
        for (let i = 0, key; result && (key = splits[i]); i += 1) {
          if (key in result) { result = result[key]; } else {
            result = create
              ? result[key] = {}
              : undefined;
          }
        }
    
        return result;
      };
    

    Have a look at the example below:

    let obj = {
        '_id': {aa: 'aa'},
        'person': {
            '$search': {
                'fname': true
            },
            '_id': {'bb': 'bb'},
            'fname': {'cc': 'cc'}
        },
        'code': {'dd': 'dd'},
        'vnvEmpName': {'name': 'sdsdd'}
    }
    
    
    const objectifier = function (splits, create, context) {
        let result = context;
    
        for (let i = 0, key; result && (key = splits[i]); i += 1) {
          if (key in result) { result = result[key]; } else {
            result = create
              ? result[key] = {}
              : undefined;
          }
        }
    
        return result;
      };
    
    console.log(objectifier('person.$search'.split('.'), false, obj));
    
    // { fname: true }
    
    Reply

Leave a Comment