Ok, I'm going a little wrong here and I've already wasted an hour with this so hopefully one of you guys can help me.
var a = ['left','top'],
x = [];
for(i=0;i<a.length;i++) {
x.push({
a[i] : 0
});
}
How do I go about pushing a value to each of the keys inside the var a
array?
You can see my failed attempted but hopefully that will give you an insight into what I'm trying to achieve.
You have to use bracket notation:
The result will be:
Maybe instead of an array of objects, you just want one object with two properties:
and
This will result in
x = {left: 0, top: 0}
.You may use:
Array.prototype.map()
Array.prototype.reduce()
Arrow functions
Comma operator
To create array of objects:
Demo:
Or if you wants to create a single object from values of arrays:
Demo: