1

Why is it giving this error when i surround the key with quotation marks?:

data[0].push({ "col" + (i+1): results.data[i].id});

1 Answer 1

3

Try using brackets (computed property name from ES6):

data[0].push({ ["col" + (i+1)]: results.data[i].id});

EDIT: If you are able to use ES6, you can also use template literal, as suggested by Ismael:

data[0].push({ [`col${i+1}`]: results.data[i].id});
Sign up to request clarification or add additional context in comments.

2 Comments

If it is to use ES6, why not { `col${i + 1}`: ... }? And I'm almost sure that this works in any version of JavaScript.
Yes, with template literal, it still better. Nice suggestion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.