Passing in dynamic key:value pairs to an object literal? [duplicate]
Solution 1:
EDIT: Use var obj = {}; obj[key] = chunks[i];
Because ECMAScript treats the key
in this {key:1}
as literal.
Solution 2:
ES2015 (via Babel) Supports dynamic keys:
const Parameters=[];
const len = chunks.length;
for (let i = 0; i < len; i++) {
const key = `key_${i}`;
obj = { [key] : chunks[i]};
Parameters.push(obj);
}
(note the brackets around the key)
Or better yet:
const Parameters = chunks.map((c, i) => ({ [`key_${i}`]: c }));