简体   繁体   中英

JavaScript acrobatics: How to declare an array, elements, and properties in one string

Given the following code:

eval('(mapping.' + binding.field + ' = eval("extVar") )');

where binding.field = "LPP[0].price" (a string) and extVar is the same value of "LPP[0].price"

How could you program the statement above to end up with:

mapping.LPP[0].price = LPP[0].price;

when mapping.LPP isn't defined yet? This code cycles through the Microsoft JQuery templates pulling variable names as strings from the tempaltes. When we reference properties in arrays, it throws up based on this code. It says that mapping.LPP[0] isn't defined.

if(!mapping.LPP) mapping.LPP = [];
mapping.LPP[0].price = LPP[0].price;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM