views:

124

answers:

4

Note: No jQuery
How could i do something like this:

var array = new Array();
array[name] = "Tom";
array[age] = 15;
foreach(array as key=>value){
    alert(key + " = " + value);
}
+4  A: 

First of all, you should call it obj or person instead of array; an array is a sequence of similar elements, not a single object.

You can do it like this:

var person = new Object();
person['name'] = "Tom";
person['age'] = 15;
for (var key in person) {
    if(!person.hasOwnProperty(key)) continue;     //Skip base props like toString

    alert(key + " = " + person[key]);
}

You can also initialize the object using properties, like this:

person.name = "Tom";
person.age = 15;

You can also use JavaScript object literal syntax:

var person = { name: "Tom", age: 15 };
SLaks
Does this work in all browsers? I've had problems with IE complaining about `for (var key in array) { .. }` type code but it's been awhile since I haven't used jQuery's $.each for my looping so not sure if it's still applicable. Just curious :)
Bartek
First, there's no need to use an array as an object is what is required (whatever the question might say). Second, that isn't JSON syntax, it's JavaScript object literal syntax. They are not the same, and the example you give is not valid JSON.
NickFitz
@Nickfitz: I answered the question is asked. You're right that that isn't an array and shouldn't be called an array, which is why I said so at the bottom.
SLaks
@SLaks: I feel that if the question as asked is itself wrong, it's better to say so at the outset to avoid perpetuating a misunderstanding; people often fail to notice things at the end. Still, YMMV :-)
NickFitz
+3  A: 

This will work in your simple example scenario:

for (var key in array) {
  alert(key + " = " + array[key]);
}

For general use, it's recommended that you test to be sure that the property hasn't been grafted onto the object somewhere else in the inheritance chain:

for (var key in array) {
  if (array.hasOwnProperty(key)) {
    alert(key + " = " + array[key]);
  }
}
Dave Ward
+3  A: 

Use a javascript object

var object = {};
object.name = "Tom";
object.age = 15;
for ( var i in object ) {
    console.log(i+' = '+ object[i]);
}
PetersenDidIt
absolutely - JavaScript arrays are NOT associative - http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
Russ Cam
+1  A: 

First, you don't want an array, you want an object. PHP's idea of what constitutes an array is frankly a little weird.

var stuff = {
    name: "Tom",
    age: 15
};

/* Note: you could also have done
var stuff = {};
stuff.name = "Tom";
stuff.age = 15;

// or

var stuff = {};
stuff["name"] = "Tom";
stuff["age"] = 15;

*/

for (var key in stuff) {
    alert(key + " = " + stuff[key];
}
NickFitz
JavaScript's idea is a little weird, too, as it does allow object-like properties in addition to numbered elements. Of course, you can hang properties off of just about anything in JavaScript--including functions.
Nosredna