0

I know that javascript doesn`t support assoc arrays, but I am curious and I played with them. And I came up with this strange behavior. Once the array is accessed as a whole, then I will get []. But if I access it with the key, can I get an actual value like in assoc array. So what is causing this Schrödinger behavior?

var test = [];

test["ab"] = {s:1};
test["bc"] = {s:2};

//returns []
console.log(test);

//return object {s:1};
console.log(test["bc"]);
Bjørson Bjørson
  • 1,583
  • 1
  • 17
  • 31
  • 7
    Arrays are Objects, you are just assigning the "ab" and "bc" properties to it, but not to its iterables, when you console.log it is calling the Array's toString function and not the Object one. – Mojimi Dec 04 '18 at 16:16
  • 1
    Possible duplicate of [Why does a string index in a javascript array not increase the length size?](https://stackoverflow.com/questions/9526860/why-does-a-string-index-in-a-javascript-array-not-increase-the-length-size) – Ivar Dec 04 '18 at 16:19
  • If you would add `console.log(test.length);` it would print `0`, because you did not put any items in it using *numeric indexes*. Array method toString() makes use of the array `.length` property. – Peter B Dec 04 '18 at 16:28

1 Answers1

1

'Associative array' is very much a PHP thing. The syntax that you're using is simply object access.

This:

const foo = { prop1: 'value1', prop2: 'value2' };
console.log(foo.prop1]);

Is the same as:

const foo = { prop1: 'value1', prop2: 'value2' };
console.log(foo['prop1']);

The benefit of the second syntax is that you can use other expressions to access the property.

const propName = 'prop1';
console.log(foo[propName]);

You are right that that's somewhat similar to PHP's associative arrays, but there's also obvious differences. One of them is that you can count PHP's associative arrays:

echo count($arr);

But you can't treat Javascript's objects the same. This won't work:

console.log(arr.length);

With javascript you can't also not really loop through a mixture of properties and array values. This is possible in PHP, but not javascript:

$arr = [ 'prop' => 'value', 5];
foreac($arr as $item) echo $item; // outputs 'value' and 5

In javascript, this might look like this:

const arr = [5];
arr.prop = 'value';

But you'll need 2 distinct loops to go through the array values and it's properties:

for(const item of arr) console.log(item);
for(const prop of Object.values(arr)) console.log(prop);

Lastly, a big difference is that Javascript's arrays are dense, and PHP's associative arrays are sparse. PHP's arrays really behave more like hashmaps than arrays. PHP's arrays keep track of their count, and can have both members that have an integer and a string index.

The closest thing Javascript actually has is new Map(). PHP doesn't have a 'true' array like Javacript does. (depending on your definition of array)

Evert
  • 93,428
  • 18
  • 118
  • 189
  • This answer should be updated at the top to highlight arrays and objects are not the same just you can use the `[]` operator to access object properties (that is actually what is happening) it's like creating an object in C and explicitly providing `[]` operator method `RETURNTYPE operator [](int i)` – Barkermn01 Oct 13 '21 at 12:51