-3

If I have an array:

const arr = ['this', 'is', 'my', 'test'];

How can I dynamically do

myObj[arr[0]][arr[1]][arr[2]]
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Related: [Accessing nested JavaScript objects and arrays by string path](https://stackoverflow.com/q/6491463/4642212). – Sebastian Simon Apr 09 '21 at 01:10
  • 2
    [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+access+nested+properties+by+array+path) of [Access nested object dynamically by using array of string as a path](https://stackoverflow.com/q/47550857/4642212). – Sebastian Simon Apr 09 '21 at 01:12
  • it is not clear what you want. Do you want to map an array field by field into an object? –  Apr 09 '21 at 01:12
  • @Lynx242 i need to do this programmatically myObj['this']['is']['my']['test'] – user3858744 Apr 09 '21 at 01:37

1 Answers1

1

I am not sure if this is what you are looking for? enter link description here

const arr = ['this', 'is', 'my', 'test'];
var Obj = arr.join(" ");
console.log(Obj)

--------------------------------please see this as updated ---------------------

const arr = ['this', 'is', 'my', 'test'];

var Obj = [];
var i;
for (i = 0; i < arr.length; i++) {
  Obj.push([arr[i]])
}
arrickx
  • 69
  • 1
  • 6