0

Given an array of objects:

const items = [{ documents: { childDocument1: '123', childDocument2: '456' }, ... }];

And given a string:

const stringForDynamicSearching = "['documents']['childDocument1']";

I'd like to get the first item childDocument1 from this string, such as:

items[0][stringForDynamicSearching]; 
// I know that this will be read as:
// items[0]["['documents']['childDocument1']"]; 
// And this will not run correctly, since the ideal code would be:
// items[0]['documents']['childDocument1']; 

But I need this to be dynamic from a string. Is it possible?

  • 1
    You should be able to modify the code in the duplicate to work with your format, or convert the string first e.g. `stringForDynamicSearching.split(/[[\]']+/).filter(Boolean).join('.')` – Nick Sep 30 '22 at 00:33

0 Answers0