Skip to main content
Active reading [<https://en.wikipedia.org/wiki/JavaScript>]. Fixed the question formation - see e.g. <https://www.youtube.com/watch?v=t4yWEt0OSpg&t=1m49s> (see also <https://www.youtube.com/watch?v=kS5NfSzXfrI> (QUASM)).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Javascript JavaScript foreach loop on an associative array object

Why is my for for-each loop is not iterating over my JavaScript associative array object?

// definingDefining an array
var array = [];

// assigningAssigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expectedExpected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

Javascript foreach loop on associative array object

Why my for for-each loop is not iterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

JavaScript foreach loop on an associative array object

Why is my for for-each loop not iterating over my JavaScript associative array object?

// Defining an array
var array = [];

// Assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// Expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

Why my for for-each loop is not iterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(iarray[i]);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

Why my for for-each loop is not iterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(i);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

Why my for for-each loop is not iterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

added 79 characters in body; edited tags
Source Link
Szymon Toda
  • 4.5k
  • 13
  • 45
  • 62

Why my for for-each loop is not iterates even onceiterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array ["Main"]array["Main"] = "Main page";
array ["Guide"]array["Guide"] = "Guide page";
array ["Articles"]array["Articles"] = "Articles page";
array ["Forum"]array["Forum"] = "Forum board";

// expected output: 4loop over every item,
// butyet gettingit 0logs only "last" assigned value - "Forum"
console.infofor (var i = 0; i < array.lengthlength; i++) {
    console.log(i);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

Why my for loop is not iterates even once?

// defining array
var array = [];

// assigning values to corresponding keys
array ["Main"] = "Main page";
array ["Guide"] = "Guide page";
array ["Articles"] = "Articles page";
array ["Forum"] = "Forum board";

// expected output: 4, but getting 0
console.info(array.length);

Why my for for-each loop is not iterating over my JavaScript associative array object?

// defining an array
var array = [];

// assigning values to corresponding keys
array["Main"] = "Main page";
array["Guide"] = "Guide page";
array["Articles"] = "Articles page";
array["Forum"] = "Forum board";

// expected: loop over every item,
// yet it logs only "last" assigned value - "Forum"
for (var i = 0; i < array.length; i++) {
    console.log(i);
}

EDIT: jQuery each() could be helpful: https://api.jquery.com/jQuery.each/

code cleanup
Source Link
Szymon Toda
  • 4.5k
  • 13
  • 45
  • 62
Loading
Source Link
Szymon Toda
  • 4.5k
  • 13
  • 45
  • 62
Loading