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.
Also in JavaScript:
- javascript unknown number of parameters
- javascript json decode
- react createelement
- python serialization json marshmallow
- smtpjs attachment
- knex js join schema
- how to delete an element from an array in javascript
- shopware 5 subscribe to theme collect javascript
- javascript add div to body with class
- animejs reduce the speed
- res object anatomy nodejs
- vscode default indent type
- nodejs await inside map
- after load page jquery
- javascript .tolowercase
- reverse geocoding javascript map
- in if condition how to set alert music in javascript
- js call and apply
- react link to another page
- redirect script javascript
- react native flatlist pull to refresh
- javascript call() & apply() vs bind()?
- change object key name javascript es6
- npm windows shocut