I am trying to achieve a dynamic variable out of string value. The dynamic variable is the nested object. I used eval method of Javascript for that purpose. It worked as I expected.
To be more precise, here is the example:
var transport = [];
transport.vehicles[0].car.brake[2] = ['diskbrake','drumbrake'];
var myString = "transport.vehicles[0].car.brake[2]";
var valueOfArray = eval(myString);
console.log(valueOfArray);
Here, the console log gives me the desired output. But, I need to achieve something like dynamic update of 'transport' variable when I add new item to the "valueOfArray" variable.
valueOfArray.push('air brakes');
When I push the new value, it should also update 'transport' object.
Any guidance or suggestions would be very appreciable.
Thank you.
evalis bad practice. Search eval is evil for more infotransport.vehicles[0].car.brake[2]refers to an array, yourvalueOfArrayvariable refers to that same array instance (not a copy of it), so if you usevalueOfArray.push()you've added an item totransport.vehicles[0].car.brake[2].var a = []; a[2].object.property.value. You will have to define these intermediate values before using themtransport.vehicles[0].car.brake[2]? this is easy and simple and error diagnosed.actually,resolve so deep path & retrieve data should be the work of thetemplateengine.