1

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.

element119
  • 7,475
  • 8
  • 51
  • 74

4 Answers4

2

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
SpoonNZ
  • 3,780
  • 1
  • 20
  • 25
1

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();
Ry-
  • 218,210
  • 55
  • 464
  • 476
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 4
    Arrays and "associative arrays" are not different in JS. All arrays in JavaScript are just objects with a special overloaded `[]=` 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. –  Jan 04 '12 at 02:52
  • I simplified the picture a little bit. :-) – Sergio Tulentsev Jan 04 '12 at 02:54
  • 2
    Why can't you do the latter? You can set arbitrary key names in arrays as well. Everything's an object. – Zirak Jan 04 '12 at 03:03
  • Well, you can, but when you later will iterate the array, it will appear empty. That can be a surprise for beginners :-) – Sergio Tulentsev Jan 04 '12 at 03:08
  • So the real answer here is that either option would make the code work because the main thing is you have to actually set `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.) – nnnnnn Jan 04 '12 at 03:39
0

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.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • An [associative array](http://en.wikipedia.org/wiki/Associative_array) is also known as a Map (or Hash), which is a property of objects in JavaScript. I prefer the term "Map" for this reason and, good coding dictates an Array-object is used like an Array, however it is very easy to blur the line in JS :) –  Jan 04 '12 at 02:47
  • @pst: So basically you are saying that "_associative array_" is "_object_" in JavaScript (but first lets distinguish `new Array()` from `new Object()`)? – Tadeck Jan 04 '12 at 02:56
  • That is correct. All objects in JavaScript function as Associative Arrays (or Maps), with the mapping String -> Any. An Array in JavaScript are a specialize subtype of an Object. `[] instanceof Array` and `[] instanceof Object` are both true. `{} instaneof Array` is false. –  Jan 04 '12 at 03:01
  • The "difference" is that array(-object)s have a special `[]=` 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) :-) –  Jan 04 '12 at 03:04
  • @pst: Ok, so let me be clear about what I was trying to say :) By "_array_" I understand instance of `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. – Tadeck Jan 04 '12 at 03:12
0

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]:

  1. Have not been set.
  2. 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.

Community
  • 1
  • 1