0

I have a multi-dimensional object data, and I want to fetch a given branch of it via a string. Example:

data = {
    title: 'Hi',
    topics: {
        title: 'Yey',
        general: {       
            topics: {
            description: 'Desc'
        }
    }
}

The dimensions of the object are variable. What I want is to be able to import the whole object, and then dynamically pick the branch I want, like so:

import data from './dataFile'

let branchString = 'data.topics.general'
let myBranch = data + branchString // this obviously doesn't work

Thanks in advance.

Anonymous
  • 850
  • 2
  • 12
  • 26
  • 2
    Fun side fact: i flagged another question as the same duplicate like 5 minutes ago – ASDFGerte May 23 '18 at 23:57
  • The duplicate answer is not satisfying. A fast answer would be : let myBranch= eval(branchString.replace(/\./gm,'"]["').replace(/\"]\[/,'[')+'"]'); – nAviD May 24 '18 at 00:55

0 Answers0