How to add object to arrays?

A

aliensite

<html>
<body>
<script>
// object constructor
function names(fname,lname){
this.fname=fname;
this.lname=lname
}
// object arrays
list = new Array()
list[0]=new names("Dave","Smith")
list[1]=new names("Mary","Brown")

//add object ("Sue","Wood") to arrays
// How?

// list fname
for(var i=0;i<list.length;i++){
alert(list.fname)
}
</script>
</body>
</html>
 
D

Douglas Crockford

// object constructor
function names(fname,lname){
this.fname=fname;
this.lname=lname
}
// object arrays
list = new Array()
list[0]=new names("Dave","Smith")
list[1]=new names("Mary","Brown")


If the objects lack methods, it is better to simply use the literal object
notation.

var list = [
{fname: "Dave", lname: "Smith"},
{fname: "Mary", lname: "Brown"}];
//add object ("Sue","Wood") to arrays
// How?

list.push({fname: "Sue", lname: "Brown"});

http://www.JSON.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top