Good Idea or Bad Practise

R

Reiver

I am creating a nuber of javaScript Arrays from a sql database via
php. It is vey convenient for me to use the sql primary index to form
the Array index, Now so far everything works perfectly, however is
this tactic bad practise that is likely to fail at some point in some
browser somewhere???

a small example of what i am doing is below

var vals = new Array();
vals[34]='a';
vals[44]='b';
vals[54]='c';
vals[134]='d';
vals[234]='e';
vals[26]='f';

I do appriciate i could use a dimensional (is that what its called)
array such as
var vals = new Array();
vals[0] = new Array(34,'a');
vals[1] = new Array(44,'b');

etc.
however my first example works and is far more convenient!


alert(vals[134]);
 
T

Thomas 'PointedEars' Lahn

Reiver said:
I am creating a nuber of javaScript Arrays from a sql database via
php. It is vey convenient for me to use the sql primary index to form
the Array index, Now so far everything works perfectly, however is
this tactic bad practise that is likely to fail at some point in some
browser somewhere???

No. (Your Dot, Shift and Question Mark keys appear to be borken.)
[...]
I do appriciate i could use a dimensional (is that what its called)

There are no built-in multi-dimensional arrays in ECMAScript
implementations, but you can emulate them:
array such as
var vals = new Array();
vals[0] = new Array(34,'a');
vals[1] = new Array(44,'b');

A more concise, semantically equal, and equally compatible form is

var vals = new Array(
new Array(34, 'a'),
new Array(44, 'b'));

(you can even omit the `new' here, since the Array constructor also works as
a factory).

Or you can use Array initializers instead:

var vals = [[34, 'a'], [44, 'b']];

See also said:
however my first example works and is far more convenient!

alert(vals[134]);

Then you should do this. (Use window.alert() instead.)

We have discussed this numerous times already. Please read and heed
<http://jibbering.com/faq/> pp.


PointedEars
 
L

Lasse Reichstein Nielsen

Reiver said:
I am creating a nuber of javaScript Arrays from a sql database via
php. It is vey convenient for me to use the sql primary index to form
the Array index,

Why does it need to be an array, then? Why not just an object.
You will still have the same indices, but you won't have the
length property - but it doesn't seem like you would need it.
Now so far everything works perfectly, however is
this tactic bad practise that is likely to fail at some point in some
browser somewhere???

That depends entirely on what you try to do with the array.
Creating it is no problem.

a small example of what i am doing is below

var vals = new Array();
vals[34]='a';
vals[44]='b';
vals[54]='c';
vals[134]='d';
vals[234]='e';
vals[26]='f';

Perfectly valid. Again, I would use "new Object()" instead of Array unless
I needed the length property.
I do appriciate i could use a dimensional (is that what its called)
array such as
var vals = new Array();
vals[0] = new Array(34,'a');
vals[1] = new Array(44,'b');

That could work too. It would require linear time to look up an
element given its key or possibly logarithmic, if you sort it by key),
instead of constant time for a well-implemented object using the key
as property name. (I say "well-implemented", because IE has had linear
lookup on its objects - I don't know what the current state of IE6 is).
etc.
however my first example works and is far more convenient!
Absolutely.

alert(vals[134]);

Easy.
The problem comes if you need to go through all the entries. There is
no good way to do that (using "for(..in..)" to range over the defined
properties is brittle - someone might extend Array.prototype).

If I needed that, I would have two structures: one object where I look
up the rows by key, and one array containing all the keys (probably
sorted).

/L
 
J

Jeremy J Starcher

On Fri, 16 Jan 2009 04:53:33 -0800, Legaev Andrey wrote:

Please don't top-post.
Why won't you try to use json_encode?

JSON is great for moving data around.
Since JSON is based on the *J*ava*S*cript *O*bject *N*otation, within
Javascript source, simply create an object literal.

(JSON has slightly stricter rules that Javascript objects, for
compatibility with decoders and to eliminate possible ambiguity. Valid
JSON will always be valid Javascript.)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top