简体   繁体   中英

Variable as key in value pair JavaScript

Hi I am having trouble inserting my variable from controller in java script key value pair. Having real trouble reading debug in F12. Value is value but also is key and key is value.

Can't you just do: inputRegions.key = myVariable and inputRegions.Value = myVariable2 ?

Sorry if it's a stupid question.

var inputRegions = [{ parsedData.Item1 : "#FFF000" }];

or even better

var cdata = { parsedData.Item1 : "#FFF000"} 
var map = new Object(); // or var map = {};
map[myKey1] = myObj1;
map[myKey2] = myObj2;

Current generally available JavaScript engines only allow static strings in object literals, thus you need temporary variables and a lot of boilerplate:

> var inputRegions  = [];
undefined
> var tmp = {};
undefined
> tmp["parsedData.Item1"] = "#FFF000";
'#FFF000'
> inputRegions.push(tmp);
1
> console.log(inputRegions);
[ { 'parsedData.Item1': '#FFF000' } ]
undefined

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