0

In PHP I can loop through an array with a foreach as loop to examine the contents of the array

$array = array(...);
foreach($array as $K=>$V){
    echo $K." = ".$V;
}

I would like to do something similar to inspect my javascript array

var array = new Array();
foreach(array ...){
    window.alert(...);
}

how can I do this?

EDIT

I should have noted that I need both the keyname AND the value

EDIT AGAIN

var $_SESSION = new Array();
$_SESSION['pin'] = "asdf";
$_SESSION['something'] = "1234";
$_SESSION['something_else'] = "123";

here's an example of my array^

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • Just use a "normal" `for` loop: `for(var i = 0; i < array.length; i++){ alert(array[i]); }` – gen_Eric Mar 18 '14 at 20:54
  • JavaScript doesn't have "associative arrays". What *exactly* is it that you have? – gen_Eric Mar 18 '14 at 20:59
  • @RocketHazmat I posted an example of what I mean by "Associative array" This is pretty much what I'm working with. I need to window.alert each index with both the keyname and the value. – I wrestled a bear once. Mar 18 '14 at 21:04
  • That's what you have in PHP! I was asking what you have in *JavaScript*. "Associative arrays" *don't exist* in JavaScript (the term is not used in that language). I'm assuming you have an *object* (which is *not* an array). Objects and Arrays are handled differently in JavaScript. – gen_Eric Mar 18 '14 at 21:06
  • @RocketHazmat no I know $_SESSION is a PHP thing but I'm using as the name of my JS array, as it holds the same data. – I wrestled a bear once. Mar 18 '14 at 21:07
  • How are you storing that data in JavaScript? Can you show us that? I'm fairly certain that the "array" you have is not what you think it is. – gen_Eric Mar 18 '14 at 21:08
  • @RocketHazmat the code above is JS not PHP – I wrestled a bear once. Mar 18 '14 at 21:08
  • Indeed it is (I didn't notice the 1st time), though what you have created is *not* an "associative array". You have created an array (with 0 elements) in it, then you added 3 properties to it. This is not the correct way to create the data structure you want. You want to create an object. `var $_SESSION = {}; $_SESSION['pin'] = "asdf";` (or `var $_SESSION = {pin: 'asdf'};`). – gen_Eric Mar 18 '14 at 21:11
  • it was just an example, I'm working with a javascript "class".. putting "class" in quotes since we're being technical.. – I wrestled a bear once. Mar 18 '14 at 21:14

3 Answers3

2

basically you would do something like this:

for(var i in array) {
     if(array.hasOwnProperty(i)){
          alert(array[i]);
     }
}

EDITED: to check for presence of key in object.

deweyredman
  • 1,440
  • 1
  • 9
  • 12
1
function logArrayElements(element, index, array) {
    console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9

(taken from here)

Note that this isn't compatible with IE8 or lower. There simply was no foreach control in older javascript versions.

Blenderer
  • 682
  • 1
  • 5
  • 17
0
for (var a = 0, b = array.length; a < b; a++) {
    alert(array[a]);
}

Defining b = array.length speeds up the loop as a < array.length would re-check the length of the array on each iteration.

Here's why you shouldn't use for ... in.

Community
  • 1
  • 1
Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44