Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Access nested objects and arrays using string path

//to get
const resolvePath = (object, path) => path.split('.').reduce((o, p) => o?.[p], object)

//to get, returning a default value if not found
const resolvePath = (object, path, defaultValue) => path.split('.').reduce((o, p) => ?.[p] ?? defaultValue, object)

//to set
const setPath = (object, path, value) => path.split('.').reduce((o,p,i) => o[p] = path.split('.').length === ++i ? value : o[p] || {}, object)
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to install react-router-dom in react js 
Javascript :: Nested comparison operator in Javascript 
Javascript :: Literal string with a variable inserted 
Javascript :: react native icons in one file 
Javascript :: React Liked Component 
Javascript :: how to test emited method from child component vue js 
Javascript :: javascript swap 
Javascript :: safari technology 
Javascript :: $("#right-button").click(function() { event.preventDefault(); $(".table-responsive").animate( { scrollLeft: "+=300px" }, "slow" ); }); 
Javascript :: react-router-dom status code 301 
Javascript :: format currency javascript 
Javascript :: js object percorrer 
Javascript :: Add React Router to React Redux CRUD App 
Javascript :: access object data 
Javascript :: checkout code 
Javascript :: Replacing Specific word from url, replacing url 
Javascript :: remove console messages of react-i18next 
Javascript :: multiple comparison javascript 
Javascript :: ios ad mobs 
Javascript :: createSearchParams 
Javascript :: Arrow Function Shorthand javascript 
Javascript :: functional-javascript-workshop solutions 
Javascript :: javascript replace url on sentence as achor 
Javascript :: discord.js get message content 
Javascript :: react native asyncstorage getitem 
Javascript :: Return the N-th value of the Fibonacci sequence 
Javascript :: fiffo in javascript 
Javascript :: ajax status update switch toggle 
Javascript :: upload image in react next js authentication 
Javascript :: dynamic data fetching in datatable.net 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =