Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

access to nested properties on javascript using property names

const getNestedObject = (nestedObj, pathArr) => {
    return pathArr.reduce((obj, key) =>
        (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj);
}

// pass in your object structure as array elements
const name = getNestedObject(user, ['personalInfo', 'name']);

// to access nested array, just pass in array index as an element the path array.
const city = getNestedObject(user, ['personalInfo', 'addresses', 0, 'city']);
// this will return the city from the first address item.
Comment

PREVIOUS NEXT
Code Example
Javascript :: react multiple classnames 
Javascript :: download file in react 
Javascript :: javascript foreach loop array 
Javascript :: react native build 
Javascript :: get string before specific character nodejs 
Javascript :: how to get json array response in retrofit 
Javascript :: best javascript books 
Javascript :: format phone number javascript 
Javascript :: React social login button 
Javascript :: react native add bottom tab and drawer menu 
Javascript :: drag drop in blazor 
Javascript :: settings.json in vscode 
Javascript :: Searchkick::ImportError: {"type"="cluster_block_exception" 
Javascript :: uncaught exception javascript 
Javascript :: spray scala JSON formatter override def read examples 
Javascript :: react classname 
Javascript :: js hex encode 
Javascript :: nodejs class template export 
Javascript :: ajax async call with wall all 
Javascript :: monaco editor no numbers 
Javascript :: header react native 
Javascript :: table to pdf javascript 
Javascript :: how to transform object in string with scaped 
Javascript :: jquery slider get value on change 
Javascript :: express-session deprecated undefined resave option; provide resave option index.js:17:9 
Javascript :: ejs docs 
Javascript :: html js hide or show iframe 
Javascript :: bcrypt create encrypted password 
Javascript :: button ref react 
Javascript :: nextjs override page route 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =