You have to use bracket notation:
var obj = {};
obj[a[i]] = 0;
x.push(obj);
The result will be:
x = [{left: 0}, {top: 0}];
Maybe instead of an array of objects, you just want one object with two properties:
var x = {};
and
x[a[i]] = 0;
This will result in x = {left: 0, top: 0}
.
More Related Contents:
- Array.push() if does not exist?
- Do objects pushed into an array in javascript deep or shallow copy?
- Declare an empty two-dimensional array in Javascript?
- push() a two-dimensional array
- JavaScript code to fetch range of dates from array of objects depending on one attribute of the array using array.reduce()
- For-each over an array in JavaScript
- Checking if a key exists in a JavaScript object?
- How do I check if a variable is an array in JavaScript?
- How to find the sum of an array of numbers
- Why does typeof array with objects return “object” and not “array”? [duplicate]
- How to initialize an array’s length in JavaScript?
- How can I access and process nested objects, arrays or JSON?
- Better way to sum a property value in an array
- Remove last item from array
- javascript extract certain properties from all objects in array
- Check if object value exists within a Javascript array of objects and if not add a new object to array
- Is a JavaScript array index a string or an integer?
- Convert javascript object or array to json for ajax data
- Obtain smallest value from array in Javascript?
- No semicolon before [] is causing error in JavaScript
- How do I remove objects from a JavaScript associative array?
- Filter strings in Array based on content (filter search value)
- How to merge multiple array of object by ID in javascript?
- How to convert an array of key-value tuples into an object
- Extract certain properties from all objects in array
- Checking for duplicate strings in JavaScript array
- Sort an array to have specific items first in the array
- Sort an array of objects by multiple properties
- Using setInterval() with a .map method in array and returning as a promise
- Lodash create collection from duplicate object keys