javascript list object

I got two quick and dirty ways to build a list/array type of object in js:

1. javascript object literals

2. javascript array literals

In the first way, to build a javascript object literal: var listObj = new Object();

To extend the list, you don’t need to call any methods in javascript object, instead simply do: listObj[“id4″] = {name:”tsi chuan”, phone:”312-222-222″};

It’s not easy to figure out how many objects are within the current list.

In the second way,

var listArr = new Array();

listArr[0] = {name:”tsi chuan”, phone:”312-222-222″};

listArr[2] = {name:”tsi chuan NEW”, phone:”312-222-333″};
//listArr.length will be 3.
It’s easy to sort the array list by index number and to figure out how many objects are really inside the list, like so:

if(listArr[2][“name”]!=undefined){//the obj exists.}

Since we are not using any methods on the objects, there is no way to keep track of the length property of the array object once this approach is used.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: