Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find property in nested object

const data = {
  "user": {
    "name": "Zahid Hasan",
    "email": "zahidhasan065@gmail.com"
  },
}

const findPropFromObj = (object, key) => {
  if (object.hasOwnProperty(key)) {
    return object[key];
  }

  for (const k of Object.keys(object)) {
    if (typeof object[k] === "object") {
      const o = findPropFromObj(object[k], key);
      if (o !== null && typeof o !== 'undefined')
        return o;
    }
  }

  return null;
}

console.log(findPropFromObj(data, 'email'))
Comment

PREVIOUS NEXT
Code Example
Javascript :: function inside object javascript 
Javascript :: javascript random number not decimal 
Javascript :: binary tree implementation javascript 
Javascript :: js get words first letter 
Javascript :: dynamically change meta tags javascript 
Javascript :: axios get 
Javascript :: express minify html 
Javascript :: mapdispatchtoprops 
Javascript :: join 2 array in javascript 
Javascript :: socket.io cors 
Javascript :: completely remove duplicate element from the array 
Javascript :: cubic root javascript 
Javascript :: javascript delay action 
Javascript :: what is node.js 
Javascript :: tochararray in javascript 
Javascript :: how to check whether a string contains a substring in javascript 
Javascript :: Quick Git Setup 
Javascript :: next js build command 
Javascript :: siwtch case javascript 
Javascript :: momentjs utcoffset 
Javascript :: create loop to specific length react 
Javascript :: check date js 
Javascript :: javascript copy content of one div to another 
Javascript :: get n random items from array javascript 
Javascript :: node.js name of file 
Javascript :: get audio duration node js 
Javascript :: reverse a string in javascript 
Javascript :: jquery filter data 
Javascript :: angular remove index of array 
Javascript :: find text in label jquery 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =