0

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"} 
4
  • 1
    ES2015 (The next version of JS - currently not broadly supported) allows for computed keys using the [] syntax, so maybe that'll help. It looks like this: cdata = {[parsedData.Item1]:'#FFFFFF'} - more info here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Dec 4, 2015 at 12:35
  • 1
    I think you forgot to ask a question (you just say "having trouble"). Do you want to create a key name that has a dot? Dec 4, 2015 at 12:37
  • Yep it worked like you said. cdata = {[parsedData.Item1]:'#FFFFFF'} Dec 4, 2015 at 13:04
  • Value is value but also is key and key is value. <--- Deep philosophy!
    – Shomz
    Dec 4, 2015 at 13:48

2 Answers 2

0
var map = new Object(); // or var map = {};
map[myKey1] = myObj1;
map[myKey2] = myObj2;
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.