1

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);
}
1
  • 4
    You should use an Array only when your keys are sequential numbers, use an Object like var obj = {}; or var obj = {'name': 'Tom', 'age': 15}; Commented Nov 29, 2009 at 19:00

5 Answers 5

4

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 };
Sign up to request clarification or add additional context in comments.

4 Comments

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 :)
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: 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: 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 :-)
4

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]);
  }
}

Comments

3

Use a javascript object

var object = {};
object.name = "Tom";
object.age = 15;
for ( var i in object ) {
    console.log(i+' = '+ object[i]);
}

1 Comment

absolutely - JavaScript arrays are NOT associative - andrewdupont.net/2006/05/18/…
1

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];
}

1 Comment

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.
0
key=0;while(key<array.length) {
    alert(key + " = " + array.item(key));
    key++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.