I'd like to build a structure in Javascript. I think it will be an array in which each element will have multiple properties.

For example
var person = new Object();
person[0].firstName="John";
person[0].lastName="Smith";
person[0].phoneNumber="555 1234";

person[1].firstName="Jane";
person[1].lastName="Robinson";
person[1].phoneNumber="555 6789";

etc.

and then things like
document.write(person[0].firstName);

This doesn't seem to work.

What am I doing wrong? Is there a way to achieve this type of structure in Javascript?

I've looked in various tutorials but none of the examples cover this type of thing.

You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.

You have defined person as an object not as an array. You can probably ise multi-dimensional arrays for that.

In fact, this code works:
function Person(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName=lastName;
this.phoneNumber=phoneNumber;
}

person = new Object();

person[0] = new Person("John","Smith","555 1234");
person[1] = new Person("Bill","Robinson","555 6789");
person[2] = new Person("Jane","Doe","555 6753");

document.writeln("<ol>")
for (personNumber in person) {
document.writeln("<li>" + person[personNumber].lastName + ", " + person[personNumber].firstName + ": " + person[personNumber].phoneNumber + "</li>");
}
document.writeln("</ol>");

It works regardless of whether I use Array or Object. Objects and arrays seem to be synonymous - if someone would give me a clear explanation of the difference I'd much appreciate it. The trick is to make each array element an object in its own right and to have a constructor for the class.

Took a while; ho hum.

Nice work, it didn't got into my mind.
Well Arrays are objects only and collection of well defines similer objects(somewhat like SETS in maths :) ). Its's also stored in a single variable name and you need to know index number of a an elemnet to access that paricular element. On the other hand objects are your own special kind of data, a collection of methods and properties whatever. With objects you could create your own data types. And you of course need to know the variable names to access the data not index number.

Hope this has made the difference clear.

vishesh

the way you're using them, they are synonymous.

arrays store indexed values, but they can also store values by associative keys. Objects can store values by associative keys also; so, when you write person[0] = la, you're creating a key (0) which you can then use to retrieve the Person-class objects from the generic Object named person. You can store values in objects using the more object-orientated "dot" syntax, without ever creating Object classes. Methinks thats bad practice though.

I think that the main difference between objects and arrays is that objects can contain inner functions.

Generally, you should use arrays to store indexed or keyed Objects or values, and you should probably avoid using the generic "Object" entirely. It reduces the strength of the code you're writing: you never really know what "Object" has inside it, or what it does.

EDIT:

I think that the main difference between objects and arrays is that objects can contain inner functions.

...apparently not true:

var test = new Array();
test.test = function(){document.write("werd")};
test["hello"] = 45;
document.write(test["hello"]);
test.test();

does exactly what it says on the tin.

well. you certainly can't prototype, class or extend the Array object.

Come to think of it, you probably can. JavaScript is a bit of a weird one.

This article has been dead for over six months. Start a new discussion instead.