2

I am going crazy here, I have an associative array as seen below which is defined after the page finishes loading. However Array.forEach is returning undefined and I have no idea why. The array is most definitely populated during the loop. Can anyone give me any ideas? Also doesn't work with JQuery's $.each

enter image description here

Michael M.
  • 10,486
  • 9
  • 18
  • 34
penu
  • 968
  • 1
  • 9
  • 22
  • 2
    pictures don't help. add your actual code – DCR Nov 07 '22 at 02:06
  • adding my code would be a bit difficult because it reads json with ajax to populate members array. But I feel that part is not necessary, and there is no other code yet. – penu Nov 07 '22 at 02:16

1 Answers1

7

Arrays are usually a mapping of index (integer from 0 to 232 − 2, inclusive) to value. In your case you've treated the array as a dictionary e.g. key (string) to value.

You've probably done something like this:

members = new Array();
members['animerox1213'] = 'Ashima';

JavaScript allows this, after all it is still an object:

typeof members === 'object'

But instead of adding a value to the array, you've actually set a non-numeric property on the object called animerox1213. That is not how an array should be used and we can observe this by checking the size:

members.length === 0;

Consequently, forEach does not do anything as it considers it an empty array.

That said, it is enumerable with forin as it's still just an object (with enumerable properties):

for (m in members) {
  console.log(m, members[m]);
}

Consider using just an object e.g. members = {} or Map. Note especially the section Objects vs. Maps.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
xlm
  • 6,854
  • 14
  • 53
  • 55
  • When I declared the `members` variable, I wrote `members = new Array()` and then did `members[a] = b;` Why did this become an Object? – penu Nov 07 '22 at 02:08
  • js is untyped. members was an array and then you made it an associative array - or dictionary. check https://flexiple.com/javascript/associative-array-javascript/ – DCR Nov 07 '22 at 02:14
  • 1
    `typeof members === 'object'` would also be the case if `members` _was_ an array. This isn’t a test for arrays. Arrays _are_ objects; they can contain any key you want, but if they contain something other than numeric keys, you’re just misusing arrays. JS is definitely not untyped. – Sebastian Simon Nov 07 '22 at 02:14
  • @SebastianSimon yes you're right, I'm working on an edit to address that. I was too hasty in my answer – xlm Nov 07 '22 at 02:16
  • @SebastianSimon I'm failing to understand why you said *"JS is definitely not untyped"*. – Gerardo Furtado Nov 07 '22 at 02:39
  • 1
    @GerardoFurtado Because [DCR’s claim that _“js is untyped”_](/questions/74341238/i-cant-iterate-over-a-javascript-array#comment131243226_74341266) is incorrect. Every value in the language is of exactly one type, of [the possible (currently 8) types](//tc39.es/ecma262/#sec-ecmascript-language-types). – Sebastian Simon Nov 07 '22 at 02:42
  • @SebastianSimon But that's correct, dynamically typed (which JS is) can be called "untyped". Even the JS creator says that: https://twitter.com/brendaneich/status/166310376340848643 – Gerardo Furtado Nov 07 '22 at 02:43
  • 1
    @GerardoFurtado In that case, the term “untyped” has an unfortunate ambiguity, so the term is perhaps best avoided… Also, even if this just means that types are dynamic, then assigning a non-numeric property to an Array still doesn’t magically _change_ its type. It still remains an Object. And it still remains an Array. – Sebastian Simon Nov 07 '22 at 02:49
  • 1
    By the way, just to add another bit of terminology: arrays are [indexed collections](//tc39.es/ecma262/#sec-indexed-collections) whereas plain objects are [keyed collections](//tc39.es/ecma262/#sec-keyed-collections). – Sebastian Simon Nov 07 '22 at 05:56