Javascript attribute detection

how to encapsulate a method that implements the following functions

let a = element["a"]["b"]["c"]...["n"]
let a = element["a"]&&element["a"]["b"]&&element["a"]["b"]["c"]&&...&&element["a"]["b"]["c"]...["n"];
let a = checkObject(element["a"]["b"]["c"]...["n"]);
checkObject(){
    // 
}
Apr.16,2021

function checkObject(obj, paramList){
  if(paramList == null || paramList.length === 0){
    return true
  }
  var prop = paramList.shift()
  return obj[prop] !=null && checkObject(obj[prop], paramList)
}

var _obj = {
  a: {
    b: {
      c: {
        d: true
      }
    }
  }
}

console.log(checkObject(_obj, ['a', 'b', 'c']))
console.log(checkObject(_obj, ['a', 'c', 'b']))

lodash/get find out, _ .get (obj, 'a.b.c.d.e.f.grub, undefined)

Menu