This seems quite simple, but I can't figure it out.
In PHP, I could do something like this:
$number_children[reference_number - 1][$key] = value;
In Javascript, I need to do the same: set a value in an associative array, which is in a normal array.
I've tried this:
number_children[reference_number - 1][key] = $(this).val();
This didn't seem to work. What's a simple method of setting this value?
Thanks for any help in advance.
4 Answers 4
Are you initializing the arrays?:
number_children = [];
number_children[reference_number - 1] = {};
number_children[reference_number - 1][key] = $(this).val();
Perhaps a jsFiddle would help show the problem.
1 Comment
In Javascript arrays and "associative arrays" are different. In fact, Javascript people don't use the term "associative arrays", they call them "objects" (or "hashes").
So, you can do this:
number_children[reference_number - 1] = {};
number_children[reference_number - 1][key] = $(this).val();
But this can have unexpected results when you try to iterate it, due to length not being set:
number_children[reference_number - 1] = [];
number_children[reference_number - 1][key] = $(this).val();
5 Comments
[]= and length (and are thus associative arrays, or Maps, as well). However, I highly recommend keeping the ADTs and usage distinct. JS helps a little more than PHP in this aspect, although it can be abused any which way, because an Array is a subtype of a Map.number_children[reference_number - 1] equal to some sort of object with ... = {} or ... = [] before you can set properties on it with number_children[reference_number - 1][key] = ... (The difference between arrays and other objects is a secondary issue.)There is no such thing as associative array in JavaScript.
In PHP you have just arrays, which can be numerical, associative, or mixed. In JavaScript you have arrays or objects. Arrays in JavaScript are not associative.
Your code is perfectly fine if number_children is an array of objects. That means if this is array, which contains objects, which in turn have the property named as the value of key variable in your example.
5 Comments
new Array() from new Object())?[]= operator that updates the length property (and also have nice access to the Array prototype so push, etc can be called on them). However, it is still a Map of String -> Any, which is why for (var i in someArray) is "often wrong" as it won't iterate indices (properties) that have never been assigned a value. for (..in..) iterates properties in an object, which does include assigned indices (properties) for array(-objects) :-)Array, and by "object" I understand instance of Object, that is not instance of Array. The problem many people face when beginning coding in JavaScript and having only experience in PHP is: expecting instances of Array ([]) to behave as in PHP. Thus, I distinguished associative arrays from "numeric" arrays. And this was the main goal of my answer.tl;dr Post the actual problem code (with minimal required context) including any error message and/or [unexpected] symptoms.
As minitech has said: there is nothing "obviously" wrong with the code.
Anyway, I Just Learned (TM) that PHP has some form of auto-vivification. JavaScript has absolutely zero auto-vivification.
Thus,
$number_children[reference_number - 1][$key] = value;
Should work in PHP assuming that $number_children and $number_children[x]:
- Have not been set.
- Have previously been set to an "array".
Since JS does not work like this, then per SpoonNZ's or Sergio Tulentsev's answers, the object/array must be explicitly created before using the [] operator. (See their answers for an example).
Happy coding.
number_children[i]as a normal array, and not as an object:[]instead of{}.