Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   How to add object to arrays? (http://www.velocityreviews.com/forums/t874096-how-to-add-object-to-arrays.html)

aliensite 11-24-2003 09:38 PM

How to add object to arrays?
 
<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[i].fname)
}
</script>
</body>
</html>

Douglas Crockford 11-24-2003 09:43 PM

Re: How to add object to arrays?
 
> // 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



All times are GMT. The time now is 06:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.