473,387 Members | 1,549 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,387 software developers and data experts.

Object instead of array? Any problems?

I've been wrestling with a really complex page. All the data is drawn
down via SQL, the page is built via VBScript, and then controlled
through javascript.

It's a page for a travel company that shows all the properties, weeks
available, pricing, etc. for a particular area of Europe. The data
varies widely depending on the region; at times there will be 50
properties, and at other times only a half dozen.

The cross referencing of pricing information, maximum guests, etc. per
property and per date had been driving me crazy, and was requiring
what felt like 17 million arrays.

Javascript isn't my specialty, so please pardon my ignorance. I'd
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I'm concerned - it seems too easy. I'm trying to figure out what
the pitfalls are.

Is it slow? Is it incompatible with a bunch of browsers? lol - it just
seems too easy after struggling with this for a couple of days. And if
it isn't slow, is it any faster than an array search? This page has
TONS of arrays, and I'm using vbscript to actually build them into the
onclick call for each date on the page. If using Object() is an
acceptable method of doing things, it could really clean up my links.

Here is the kind of thing I am doing:

var ratetable = new Object();
ratetable["r1123"] = [800,400,300,200,0,0,0,0];
ratetable["r6543"] = [-50, -100, -150, 0, 0, 0, 0, 0];
ratetable["r2342"] = [50,50,50,50,25,25,25,25]

rvalue="r6543";
passengers=3;

alert(ratetable[rvalue][passengers-1])

In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it's a mess. This object thingy if
it is OK to use would be SOOO great.

What am I missing here?

Thanks,

Julie

Jun 7 '07 #1
14 2761
Hi Julie,

On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:
It's a page for a travel company that shows all the properties, weeks
available, pricing, etc. for a particular area of Europe. The data
varies widely depending on the region; at times there will be 50
properties, and at other times only a half dozen.

The cross referencing of pricing information, maximum guests, etc. per
property and per date had been driving me crazy, and was requiring
what felt like 17 million arrays.

Javascript isn't my specialty, so please pardon my ignorance. I'd
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I'm concerned - it seems too easy. I'm trying to figure out what
the pitfalls are.
You should use an object then! I use objects far more often then
arrays. The reason is I can index into the object by the "key" and get
the "value". This is treating a JavaScript object like a hash in many
languages.
Is it slow?
Faster than hunting through a big array.
Is it incompatible with a bunch of browsers?
All of them since a long, long time ago.
lol - it just
seems too easy after struggling with this for a couple of days.
A pleasure I'm sure! :)
And if
it isn't slow, is it any faster than an array search?
If the JavaScript engine is written well then it is faster.
This page has TONS of arrays,
How much data are you loading into this web page? Is it making the
page load noticeably slowly?
and I'm using vbscript to actually build them into the
onclick call for each date on the page. If using Object() is an
acceptable method of doing things, it could really clean up my links.

Here is the kind of thing I am doing:

var ratetable = new Object();
ratetable["r1123"] = [800,400,300,200,0,0,0,0];
ratetable["r6543"] = [-50, -100, -150, 0, 0, 0, 0, 0];
ratetable["r2342"] = [50,50,50,50,25,25,25,25]

rvalue="r6543";
passengers=3;

alert(ratetable[rvalue][passengers-1])
There is an easier ways to create objects with an object literal

var ratetable = {
r1123: [800,400,300,200,0,0,0,0],
r6543: [-50, -100, -150, 0, 0, 0, 0, 0],
r2342: [50,50,50,50,25,25,25,25]
};

In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it's a mess. This object thingy if
it is OK to use would be SOOO great.
The object thingy is ok. It is one of the best features of the
language.

Peter

Jun 7 '07 #2
On Jun 7, 1:43 pm, Peter Michaux <petermich...@gmail.comwrote:
Hi Julie,

On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:
[...]
Javascript isn't my specialty, so please pardon my ignorance. I'd
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I'm concerned - it seems too easy. I'm trying to figure out what
the pitfalls are.

You should use an object then!
[...]
Is it incompatible with a bunch of browsers?

All of them since a long, long time ago.
I think you mean *none* of them. :-)

Just about everything in javascript is an object, so if a particular
browser doesn't support objects...

lol - it just
seems too easy after struggling with this for a couple of days.

A pleasure I'm sure! :)
And if
it isn't slow, is it any faster than an array search?

If the JavaScript engine is written well then it is faster.
You can search an array using a for loop like:

var elementValue;
for (var i=0, len=someArray.length; i<len; i++){
elementValue = someArray[i];
/* do something with elementValue */
}

The object equivalent is a for..in loop:

var propertyValue;
for (var property in someObject) {
propertyValue = someObject[property];
/* do something with objectProperty or propertyValue */
}

But be aware that if someObject's prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.
--
Rob

Jun 7 '07 #3
On Jun 6, 9:33 pm, RobG <r...@iinet.net.auwrote:
On Jun 7, 1:43 pm, Peter Michaux <petermich...@gmail.comwrote:
Hi Julie,
On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:
[...]
Javascript isn't my specialty, so please pardon my ignorance. I'd
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I'm concerned - it seems too easy. I'm trying to figure out what
the pitfalls are.
You should use an object then!

[...]
Is it incompatible with a bunch of browsers?
All of them since a long, long time ago.

I think you mean *none* of them. :-)
Umm. Yes, yes I did. Thanks, Rob.

Peter

Jun 7 '07 #4
RobG wrote:
But be aware that if someObject's prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.
Adding methods or properties via the prototype property is bad practice?

I thought the general rule of thumb was you did not want to mess with
the existing JavaScript Objects' prototype chain. As in, don't augment
Array or String or whatever.

Does that mean you should only do:

var myObj = {};
myObj.attr1 = 'I am an attribute of myObj.';
myObj.func1 = function() { alert('I am a function of myObj.'); }

....?

What about how people augment String to include ltrim- and rtrim-type
functions? The FAQ even illustrates this.

Or in the case of a custom application you require all objects have the
ability to identify themselves to the calling function or expose the
context in which they were called.

How do you go about doing something like that without prototype?

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #5
ju**********@gmail.com wrote:

<snip>
In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it's a mess. This object thingy if
it is OK to use would be SOOO great.
Hi,

I know excactly how you feel! I remember my happiness when I first started
using porperties in Objects instead of arrays. Great indeed!
I still name them hashes, just as in other languages, but many in here
demand that they are named properties. ;-)

Enjoy.

Regards,
Erwin Moller
>
What am I missing here?

Thanks,

Julie
Jun 7 '07 #6
On Jun 7, 7:04 pm, -Lost <maventheextrawo...@techie.comwrote:
RobG wrote:
But be aware that if someObject's prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.

Adding methods or properties via the prototype property is bad practice?
I didn't word that very well: most consider extending the Object
prototype as bad practice always, some also shun extending Array,
particularly in libraries. Extending user-defined native objects
isn't bad, as you point out below, it's required for prototype-based
inheritance.

I thought the general rule of thumb was you did not want to mess with
the existing JavaScript Objects' prototype chain. As in, don't augment
Array or String or whatever.
It's probably OK to extend objects like String, Number and Math since
if done properly it's unlilely to have adverse effects (e.g. no one
should be using them, or objects constructed from them, in a for..in
loop). However, Array and Object should be left alone.
--
Rob

Jun 7 '07 #7
RobG wrote:
On Jun 7, 7:04 pm, -Lost <maventheextrawo...@techie.comwrote:
>RobG wrote:
>>But be aware that if someObject's prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.
Adding methods or properties via the prototype property is bad practice?

I didn't word that very well: most consider extending the Object
prototype as bad practice always, some also shun extending Array,
particularly in libraries. Extending user-defined native objects
isn't bad, as you point out below, it's required for prototype-based
inheritance.
>I thought the general rule of thumb was you did not want to mess with
the existing JavaScript Objects' prototype chain. As in, don't augment
Array or String or whatever.

It's probably OK to extend objects like String, Number and Math since
if done properly it's unlilely to have adverse effects (e.g. no one
should be using them, or objects constructed from them, in a for..in
loop). However, Array and Object should be left alone.
Duly noted. As always, thanks.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #8
to be honest, I think you are confusing objects with associative arrays

all arrays in javascript can be indexed by text strings instead of numbers,
so there is actually little difference from your point of view between an
object and an array

<ju**********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
I've been wrestling with a really complex page. All the data is drawn
down via SQL, the page is built via VBScript, and then controlled
through javascript.

It's a page for a travel company that shows all the properties, weeks
available, pricing, etc. for a particular area of Europe. The data
varies widely depending on the region; at times there will be 50
properties, and at other times only a half dozen.

The cross referencing of pricing information, maximum guests, etc. per
property and per date had been driving me crazy, and was requiring
what felt like 17 million arrays.

Javascript isn't my specialty, so please pardon my ignorance. I'd
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I'm concerned - it seems too easy. I'm trying to figure out what
the pitfalls are.

Is it slow? Is it incompatible with a bunch of browsers? lol - it just
seems too easy after struggling with this for a couple of days. And if
it isn't slow, is it any faster than an array search? This page has
TONS of arrays, and I'm using vbscript to actually build them into the
onclick call for each date on the page. If using Object() is an
acceptable method of doing things, it could really clean up my links.

Here is the kind of thing I am doing:

var ratetable = new Object();
ratetable["r1123"] = [800,400,300,200,0,0,0,0];
ratetable["r6543"] = [-50, -100, -150, 0, 0, 0, 0, 0];
ratetable["r2342"] = [50,50,50,50,25,25,25,25]

rvalue="r6543";
passengers=3;

alert(ratetable[rvalue][passengers-1])

In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it's a mess. This object thingy if
it is OK to use would be SOOO great.

What am I missing here?

Thanks,

Julie

Jun 7 '07 #9
Lee
Andy Fish said:
>
to be honest, I think you are confusing objects with associative arrays

all arrays in javascript can be indexed by text strings instead of numbers,
so there is actually little difference from your point of view between an
object and an array
No, I think you're confused.
The fact that you can seem to index arrays by text strings
is simply because arrays are Objects. It's slightly better
to use a generic Object in this way than an Array, because
the Array object has a "length" attribute and array-specific
methods that could cause confusion.
--

Jun 7 '07 #10
On Jun 7, 8:52 am, "Andy Fish" <ajf...@blueyonder.co.ukwrote:
to be honest, I think you are confusing objects with associative arrays
There isn't something called and "associative array" in JavaScript.
All objects (including arrays and functions which are objects) can be
used as an "associative array".
all arrays in javascript can be indexed by text strings instead of numbers,
This is because arrays are objects.
so there is actually little difference from your point of view between an
object and an array
If you want to use an "associative array" concept then it is
conceptually simpler to use an plain object since the arrayness of an
array object won't be utilized.

Peter

Jun 7 '07 #11
Peter Michaux wrote:
The object thingy is ok. It is one of the best features of the
language.
I agree :)

Jun 7 '07 #12
Peter Michaux wrote:
The object thingy is ok. It is one of the best features of the
language.
I agree :)

Jun 7 '07 #13
So I sent an post earlier and I either did something wrong or Google
groups ate it.

In essense it said (and then some, because I can't seem to shut
up :P):

Thanks to everyone for all your help (via the Group and through
email)!

I don't entirely understand everything you are talking about, but I
think I got the jist of it enough to get started.

In case anyone wondered, the whole point of this page is that it is
replacing the search and selection for a complicated sort of trip (for
this company, anyway). No searching will be necessary - all the
availability/pricing/properties/persons will be in a single page that
never has to reload because a search didn't yield the options the
customer was looking for. Should be easier on the server.

I'm redoing the javascript arrays/functions and stored procedures/
vbscript for the page now. I'm going to see if my client will allow me
to post the test page here. This stuff is much easier to show than it
is to explain. I'm assuming there are going to be speed issues when it
is complete (though it loads pretty quickly) and I'm wondering if I
SHOULD make certain "tables" of data into standard arrays.

Thanks again for your help. You guys are incredible.

Julie

Jun 8 '07 #14
On Jun 7, 4:55 am, RobG <r...@iinet.net.auwrote:
On Jun 7, 7:04 pm, -Lost <maventheextrawo...@techie.comwrote:
RobG wrote:
But be aware that if someObject's prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.
Adding methods or properties via the prototype property is bad practice?

I didn't word that very well: most consider extending the Object
prototype as bad practice always, some also shun extending Array,
particularly in libraries. Extending user-defined native objects
isn't bad, as you point out below, it's required for prototype-based
inheritance.
I thought the general rule of thumb was you did not want to mess with
the existing JavaScript Objects' prototype chain. As in, don't augment
Array or String or whatever.

It's probably OK to extend objects like String, Number and Math since
if done properly it's unlilely to have adverse effects (e.g. no one
should be using them, or objects constructed from them, in a for..in
loop). However, Array and Object should be left alone.
In general, as you note, there's little issue about augmenting some
built-ins through the prototype mechanism. There's been long-standing
controversy over augmentation of Array and, and in particular for,
Object.

In Javascript, the good news is that Object.prototype augmentation can
and does have and effect on all objects. The bad news is that
Object.prototype augmentation can and does have and effect on all
objects.

Concentrating on Object, the "worst" case, it seems there are the
majority who like to play it safe and avoid any interference that
might be caused by augmentation by way of Object.prototype. On the
other hand, there are those including myself, who see such avoidance
as a restriction on the capabilities which a prototyping language such
as Javascript is designed to provide.

The underlying problem that arises when Object.prototype is augmented
is that the property namespace expands, and where there was formerly
an expectation that property resolution occur only within a local
object (or within a constructed prototype chain), that no longer
pertains. In particular, "for .. in" loops give rise to delivery of
properties that are not necessarily local to the object upon which
iteration is being carried out.

Nonetheless, this circumstance is not all that different from that in
which the language is being used as intended, with prototypal
inheritance prescribed through the use of constructor prototypes for
user-created object extensions. The programmer, in general, needs to
be fully aware of the prototypal property resolution and, among other
things, be prepared to filter with "hasOwnProperty" in situations
where read resolution, including "for ... in" iteration, is to be
constrained to the immediate object.

Including a "hasOwnProperty" test or filter where required is somewhat
onerous, and not all that pretty as compared to having a clean loop.
So, if one has decided to augment Object.prototype, in my view, the
first augmentation that should be created is an iterator constructor
that will facilitate the iteration of Object objects, Array objects,
(and possibly other built-ins) and user-defined object extensions.

For my own projects, I have a personal library which provides
augmentation to Object and Array, as well as provision of other
utility services. One of the advantages of having an iteration
facility is that it is no longer necessary to choose an iteration
construct based on type of the object, e.g., Object or Array - the
iterator decides what set keys to use in the iteration, and provides
keys and values to the iteration loop in accordance as properties of
the iterator object.

So almost invariably iteration appears as:

var it = obj.iterator(); // [1]
while (it.next()) {
alert("key: " + it.k + " value:" + it.v); // Display local key,
value, e.g.
}

and seldom is seen a "for(..;..;..)" or "for (... in ...)" looping
construct.

Whereas little is lost in execution efficiency, much is to be gained
in terms of uniformity and overall augmented facility brought to hand.

____________

[1] Care is still required with the expanded namespace. _iterator, or
some such, would be a better choice of name, perhaps, to help prevent
the possibility of conflicting override within an object.
--
../rh

Jun 8 '07 #15

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

Similar topics

0
by: Arne Kösling | last post by:
Hi ! I am new to Web Services. Therefore I ve set up a PHP Installation on Windows (PHP 4.3.2 and Apache 1.3.29). I have tested PHP alone and then installed PEAR. Now I am stuck there (Before...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
3
by: Richard Cavell | last post by:
#include <gmp.h> mpz_t* p_mympz = new mpz_t; error: cannot convert '__mpz_struct*' to '__mpz_struct (*)' in initialization 1. What does that mean? 2. Given that mpz_t doesn't have a...
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...
11
by: simon | last post by:
hello, I have a form that has 10 dropdown lists in one section each of the dropdowns contain the same data what i'm looking to do is that when a user selects a value from one dropdown, change...
73
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. ...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
5
by: Eric bouxirot | last post by:
hi, i'd like to do a "cast", who seem to be very complicated in .NET. in C standard it's so easy... i explain : i have made an app based on plugin architecture in VB.NET. all work fine.. ...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.