get object subkey by path string
const locale = {
title: 'Title',
feedback: {
send: 'Send',
},
}
const path = 'feedback.send'
const brace_path = 'feedback[send]'
console.log({
first_level_key: locale['title'],
nested_by_dot: locale['feedback.send'],
direct: locale.feedback.send,
eval: eval('locale.feedback.send'),
})
console.log({ eval_template: eval(`locale.${path}`) })
const get = require('lodash.get')
console.log({ lodash_get: get(locale, path) })
const regex = /[\[\]]/
const brace_test = brace_path.match(regex)
const no_braces_test = path.match(regex)
if (no_braces_test) {
console.log({no_braces_test}, 'there is a braces')
} else {
console.log({no_braces_test}, 'no braces')
}
if (brace_test) {
console.log({brace_test}, 'there is a braces')
} else {
console.log({brace_test}, 'no braces')
}
no comments