I think my problem isn't that hard but I'm kinda new in javascript... so my question is how to make something like the following (the following doesn't work):
var t1 = {
0 : "lel",
};
var t2 = {
t1[0] : 1,
};
I think my problem isn't that hard but I'm kinda new in javascript... so my question is how to make something like the following (the following doesn't work):
var t1 = {
0 : "lel",
};
var t2 = {
t1[0] : 1,
};
You can simply use t2[t1[0]] = 1
like this:
var t2 = {};
t2[t1[0]] = 1;
Demo below:
var t1 = {
0: "lel",
};
var t2 = {};
t2[t1[0]] = 1;
console.log(t2);
With ES, you could use computed property names for it.
var t1 = { 0 : "lel" },
t2 = { [t1[0]]: 1 };
console.log(t2);
An array literal in javascript looks like this:
var a = [val1, val2, val3];
If you want to put a value from one array into another array you can do this:
var a1 = [val1, val2, val3];
var a2 = [a1[0], a1[1], a1[2]];
If you want to use the value as a key for an object you have to do:
var a = [someValue];
var b = {[a[0]]: someOtherValue};
Or if you need to support older browsers:
var b = {};
b[a[0]] = someotherValue;
Keep in mind that there are better ways to take data from one array and put it into another array or into an object look at map or reduce and similar functions. Or you could look at using a library like lodash, which is great for array and object manipulation.