V
V
Vladislav Radzimovsky2014-10-03 18:31:13
JavaScript
Vladislav Radzimovsky, 2014-10-03 18:31:13

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}

It turns out this:
Object {id: 141241, str: 12424}
Why does js see the id variable as a string? And how do you put it in there?
Thanks in advance for your replies. Link to jsfiddle (watch console)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Khirenko, 2014-10-03
@Lafafm

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;

V
Vitaly Kirenkov, 2014-10-03
@DeLaVega

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 question

Ask a Question

731 491 924 answers to any question