Answer the question
In order to leave comments, you need to log in
How to insert a variable into the first element of an associative array?
Hello!
I can hardly understand why this problem occurs, and even more so how to solve it, but the point is that when I try to insert a variable into the first element of the array:
var id = 'test', num = 141241, num2 = 12424
var arr = {id : num, 'str' : num2}
Object {id: 141241, str: 12424}
Answer the question
In order to leave comments, you need to log in
As far as I understand, you need to make the value of the id variable the arr key, if so, then like this:
var id = 'test', num = 141241, num2 = 12424;
var arr = {str: num};
arr[id] = num2;
It's not an array, it's an object. Objects are made up of keys and values.
And as far as I remember, the keys are not set in this way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question