JSFiddle - React, Tailwind, and code Playground
by Bateman
JavaScript
function getPropValue(source, path) {
const segments = path.split('.')
return segments.reduce((transientValue, key) => transientValue[key], source)
}
function pick(source, keys) {
return keys.reduce((acc, key) => ({ ...acc, [key]: getPropValue(source, key) }), {})
}
var person = {
name: 'Alice',
address: {
city: 'Tokyo'
}
}
console.log(
getPropValue(person, 'name')
)
/* const value = pick(person, ['name', 'address.city']) */
/* console.log(value) */