I have a somewhat strange question, I would like to know if it is possible to use a string as an entire reference to get a value from an object within an array.
This is my array:
const myArray = [
{name: 'element1', id: 'elementid1'},
{name: 'element2', id: 'elementid2'}
];
where myArray[0]["name"]
returns: 'element1'
Would it be possible to have this entire reference: myArray[0]["name"]
as a string: 'myArray[0]["name"]' and use it to reference to this value.
So this: getViaString
returns 'element1' with the following set up:
const getViaString = 'myArray[0]["name"]';
I have set up this fiddle as it probably explains better what I am trying to do: jsfiddle
Thanks.