0

say I have this

const data = {
   company: {
     people: [{name: "john"}, {name: "rich"}]
  }
}

obviously I can do data.company.people // array of 2

I can do also data['company']['people'] // same result

but let's say I don't know the properties (I only know data)

I can do

x = 'company'
y = 'people'
data[x][y] // this will still give me what I want

BUT what I want to know is how can I combine x & y to give me what I want because

data['company.people'] gives me undefined

and also because I'm working with async requests I need

data['company?.people'] to work too

does anyone know how this is possible? or do I need to write a function that handles this for me?

Red Baron
  • 7,181
  • 10
  • 39
  • 86

0 Answers0