473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array of objects

Can someone give me an example of how to construct an array of objects?
for example, I have:
function objFacility(id,name,adr,city,state,zip)
{
this.id = id
this.name=name
this.adr=adr
this.city=city
this.state=state
this.zip=zip
}
I would need to search the array by id then access one element
Thanks for any help
walt

Jul 15 '06 #1
2 46961
<wl*****@sigsys.netwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Can someone give me an example of how to construct an array of objects?
for example, I have:
function objFacility(id,name,adr,city,state,zip)
{
this.id = id
this.name=name
this.adr=adr
this.city=city
this.state=state
this.zip=zip
}
I would need to search the array by id then access one element
Assuming you want an arrau of those objects: ;^)

You can do this in literal notation like so:

MyArray = [
new objFacility(...),
new objFacility(...),
new objFacility(...)
];

The brackets indicate an array.

You can also do this in script like so:

MyArray = new Array();
MyArray[MyArray.length] = new objFacility(...);

In this case "MyArray.length" is just saying "make this the last element in
the array". You could also, of course, loop over something to do the same
thing:

Just as a plug. If you need to do any really work with the resulting array
of objects you might want to check out my Object Collection Ordered
abstraction object here:

http://www.depressedpress.com/Conten...ered/Index.cfm

It basically creates an array of objects as we've just done but also exposes
lots of methods for managing and ordering them. Once the object is imported
you might do this:

MyFacilities = new DP_ObCollectionOrdered("id", objFacility);

This creates the collection and tells it that only "objFacility" objects can
be stored in it. It also tells it that the "id" field will be used as the
unique identifier for the collection.

You can then add elements to the collection like this:

MyFacilities.add( new objFacility(...) );
MyFacilities.add( new objFacility(...) );
MyFacilities.add( new objFacility(...) );

Once that's done you have a lot of capabilities. To get one element you
would just do:

MyFacilities.get(FacilityID);

To get everything (as a reference to the internal array) you would do:

MyFacilities.getAll();

To get the first element in the collection you would say:

MyFacilities.getAt(0);

To move the first element down two steps (in the language of the collection
object "reduce its rank by two") you would do:

MyFacilities.demoteAt(0, 2);

To move a specific element down two steps you would say:

MyFacilities.demote(FacilityID, 2);

There are lots of similar methods for adjusting rank: promote(),
promoteAt(), swap(), swapAt(), setRank(), etc.

Finally the collection object offers a generic, but pretty powerful sorting
function called "sortByProp()". To sort the collection by Facility Name
regardless of alphabetic case you could do this:

MyFacilities.sortByProp(name, "AlphaNoCase", "asc");

To resort the list by name descending you would just do this:

MyFacilities.sortByProp(name, "AlphaNoCase", "Desc");

There are examples on the page for generating drop down select boxes and
dynamically sortable tables on the page as well.

I hope this helps,

Jim Davis
Jul 15 '06 #2
wl*****@sigsys.net wrote:
Can someone give me an example of how to construct an array of objects?
for example, I have:
function objFacility(id,name,adr,city,state,zip)
{
this.id = id
this.name=name
this.adr=adr
this.city=city
this.state=state
this.zip=zip
}
I would need to search the array by id then access one element
If you want to retrieve objects by id, then you should use an object, not an
array. Since functions are also objects, you could store the members in the
function itself.

function objFacility(id, name, adr, city, state, zip) {
return objFacility[id] = {
id: id,
name: name,
adr: adr,
city: city,
state: state,
zip: zip
}
}

objFacility('wlevine', 'Levine', '23 Skid Row', 'Springfield', 'Il', 10010);

The object can be obtained with objFacility.wlevine

http://javascript.crockford.com
Jul 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kutty Banerjee | last post by:
Hi, MyClass *myclass_=new MyClass; and MyClass::MyClass(int) and no default constructor. My object assignment obviously gives an error. So waht is the correct way to do it without using vectors...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
1
by: plau011 | last post by:
Hi all, I thought I understood .Net Framework with value types and referencetypes but apparently not. So I have this code. Excuse me if it's not 100% correct I cut and paste and shortened it a...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
4
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without...
13
by: milk242 | last post by:
I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.