Array in javascript

R

RobG

VK said:
for-in loop has nothing to do with *array*, it's an iterator over
enumerable properties of object.

Strange comment - arrays are objects. For-in works fine provided you
deal with properties that aren't numbers.

Try this to understand the difference:

var arr = [1,2,3];
arr.foo = 'bar';

for (var i=0; i<arr.length; i++) {
alert(arr);
}

for (var p in arr) {
alert(p);
}


I guess you are suggesting that if some of the properties aren't
numbers, you'll get erroneous results. But that can happen with a for
loop too and is catered for in the solutions offered above (typeof and
isNaN tests are obvious choices).

Ian's suggested method offers significant speed benefits *if* the array
length is large *and* the array is very sparse - otherwise, there is
little to recommend it.

Neither of the above conditions were suggested by the OP, but the point
is useful to understand.
 
R

Randy Webb

RobG said the following on 2/3/2006 9:11 AM:
Strange comment - arrays are objects. For-in works fine provided you
deal with properties that aren't numbers.

VK confuses the hell out of me sometimes, but I think he confuses
himself more. What I think he meant was that for-in is not limited to
arrays but can be used on any object.
 
J

John W. Kennedy

Duncan said:
RobG said:
Strange comment - arrays are objects. For-in works fine provided you
deal with properties that aren't numbers.

.... and provided you aren't too choosy about the order in which you process
the array elements: the order of iteration produced by for..in is
undefined, and can suprise people who don't realise that.

If you don't believe me, try the following:

var a = ['a', 'b', 'c'];
a[4] = 'e'; a[3] = 'd';
for(var idx in a) {
alert(idx+"="+a[idx]);
};

The alerts in IE and Firefox go in the order 0,1,2,4,3 (although a
conforming ecmascript implementation could use any order it fancied).

Note, too, that "idx" above is treated as a string, not an integer.

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
 
R

Richard Cornford

Richard Cornford wrote:
The javascript array has no potential to have multiple
dimensions and so no potential to be 'jagged'. ...
<snip>

As it happens it has just occurred to me that the concept of a
multi-dimensional array can be implemented with a single javascript
Array:-

var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}

- The result is a single array that emulates an array with infinite
dimensions. I.E. you can stick as many sets of square brackets and valid
array indexes after the array's Identifier as you want:-

var y = a[0][0][0][0][0][0][0][0][0][0] ... [0][0][0][0][0];

- and still get a valid result from the evaluation of the property
accessor. The resulting object is of course utterly useless as an array,
it will be broken by any attempt to store anything in it, and it is not
(and could not be) 'jagged'. But utterly useless as the result may be it
is one, and the only one, way that a single javascript array can appear
to be multi-dimensional.

Richard.
 
T

Thomas 'PointedEars' Lahn

John said:
Duncan said:
[...]
var a = ['a', 'b', 'c'];
a[4] = 'e'; a[3] = 'd';
for(var idx in a) {
alert(idx+"="+a[idx]);
};

The alerts in IE and Firefox go in the order 0,1,2,4,3 (although a
conforming ecmascript implementation could use any order it fancied).

Note, too, that "idx" above is treated as a string, not an integer.

However, this does not matter here. All property names are strings. In
fact, any expression as property name is subject to ToString() conversion
on property access. See ECMAScript Ed. 3, 11.2.1 and 15.4.

This means that the property accesses a["3"] and a[3], and ({3: 33})["3"]
and ({3: 33})[3] are equivalent.


PointedEars
 
V

VK

Randy said:
To me, it's not a discussion, it is a one-sided statement from
Richard showing the flaws in what you are saying.

And I'm showing the flaws in what he is saying while both are staying
away from Thomas'-like vocabulary. To me, it is a discussion. ;-)
I can answer *any* question not directly related to mutil-dimensional
arrays without using any one of those three phrases, and in fact I
wouldn't even consider using any of those terms unless it was directly
related to the question.

This loud statement is bookmarked too :)
To date, most of your points of view with regards to Array's has been
dead wrong. The funny part is watching you trying to defend that position.

It was confirmed by many, including professional programmers and
programming specialists. As it brings them automatically to the
category of "people who knows no more or even less than VK does":- my
question would be what authority besides Richard Cornford would you
accept as an authority? I'm really open for requests: except a message
form the Lord and a wiki article. The first is out of my power, the
latter is too bi..y to use (it says whatever one wants - for at least
an hour).
So exept that?
 
J

John W. Kennedy

Thomas said:
John said:
Duncan said:
[...]
var a = ['a', 'b', 'c'];
a[4] = 'e'; a[3] = 'd';
for(var idx in a) {
alert(idx+"="+a[idx]);
};

The alerts in IE and Firefox go in the order 0,1,2,4,3 (although a
conforming ecmascript implementation could use any order it fancied).
Note, too, that "idx" above is treated as a string, not an integer.

However, this does not matter here. All property names are strings. In
fact, any expression as property name is subject to ToString() conversion
on property access. See ECMAScript Ed. 3, 11.2.1 and 15.4.

This means that the property accesses a["3"] and a[3], and ({3: 33})["3"]
and ({3: 33})[3] are equivalent.

Yes, but it makes the semantics of "+" surprising.

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
 
T

Thomas 'PointedEars' Lahn

John said:
Thomas said:
John said:
Duncan Booth wrote:
[...]
var a = ['a', 'b', 'c'];
a[4] = 'e'; a[3] = 'd';
for(var idx in a) {
alert(idx+"="+a[idx]); ^^^^^^^
};

The alerts in IE and Firefox go in the order 0,1,2,4,3 (although a
conforming ecmascript implementation could use any order it fancied).
Note, too, that "idx" above is treated as a string, not an integer.
However, this does not matter here. All property names are strings. In
fact, any expression as property name is subject to ToString() conversion
on property access. See ECMAScript Ed. 3, 11.2.1 and 15.4.

This means that the property accesses a["3"] and a[3], and ({3: 33})["3"]
and ({3: 33})[3] are equivalent.

Yes, but it makes the semantics of "+" surprising.

I do not see your point, string concatenation always happens with the above
code, and nothing is attempted to be computed. Maybe you are referring to
the following solution for the OP's problem:

var
a = [1, 654, 2, 5, 489, 51, 3851, 681, 32, 5, 0],
sum = 0;

for (var idx in a)
{
sum += a[idx];
}

It also does not matter here whether `idx' refers to an integer (there are
no integers in ECMAScript implementations, all numeric values are IEEE-754
doubles), or to a string value, as it actually does. Note that it is _not_
the indexes that should be added but the array element's values they refer
to.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Jasen said:
No it is not because all arrays have a length property which tells you how
many elements they have :)

Should not that have rather been a "Yes it is because ..." answer? ;-)


PointedEars
 
V

VK

Richard said:
Richard Cornford wrote:
The javascript array has no potential to have multiple
dimensions and so no potential to be 'jagged'. ...
<snip>

As it happens it has just occurred to me that the concept of a
multi-dimensional array can be implemented with a single javascript
Array:-

var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}

- The result is a single array that emulates an array with infinite
dimensions. I.E. you can stick as many sets of square brackets and valid
array indexes after the array's Identifier as you want:-

var y = a[0][0][0][0][0][0][0][0][0][0] ... [0][0][0][0][0];

Richard, what's that? And how is it related with dimensions? It's a
single-dimensional array having 10 elements where each element value is
a reference to the containing array.
 
R

Richard Cornford

VK said:
Richard said:
Richard Cornford wrote:
The javascript array has no potential to have multiple
dimensions and so no potential to be 'jagged'. ...
<snip>

As it happens it has just occurred to me that the concept of a
multi-dimensional array can be implemented with a single
javascript Array:-

var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}

- The result is a single array that emulates an array with
infinite dimensions. I.E. you can stick as many sets of square
brackets and valid array indexes after the array's Identifier
as you want:-

var y = a[0][0][0][0][0][0][0][0][0][0] ... [0][0][0][0][0];

Richard, what's that?

It is what is known as 'intellectual rigor' (look it up).
And how is it related with dimensions? It's a
single-dimensional array having 10 elements where each
element value is a reference to the containing array.

It is an array of arrays that only employs a single javascript array.

An emulation in javascript of what can be achieved with a
multi-dimensional array in languages that have such can only be done
with an array of arrays. Requiring an array of arrays implies that any
emulation of a multi-dimensional array cannot be done with a single
javascript Array object. I have demonstrated that there is a single case
where an array of arrays can be constructed and still only employ a
single javascript Array object.

Utterly useless as this one special case may be it would be dishonest to
deny its existence. Fortunately my deduction was that since an emulation
of what can be done with a 'jagged' array in languages that support such
can only be done with an array of arrays then 'jagged' cannot be an
appropriate term to apply to any single javascript array object, and so
'jaggedness' is not a quality of javascript Arrays. As the one
exceptional case where a single javascript Array object may appear to be
multi-dimensional in no way qualifies as 'jagged' that premise is not
altered by that observation.

Richard.
 
V

VK

Richard said:
var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}
It is an array of arrays that only employs a single javascript array.

It is not: you have forgotten that in JavaScript arrays (as other
objects) are passed *by reference*. So you've created a rather
interesting (I have to admit it) circular structure where on each loop
you change the a[] array and add new reference on it into a[] itself.
But by the end you still have nothing but references to the same object
in all elements. This is why you cannot make any assignment in this
structure - it would be like to be a spectateur and the main "hero" on
funerals.
Same twist can be done with the associative array as well (with the
same useless outcome) so it is not anyhow related with the array
question.
An emulation in javascript of what can be achieved with a
multi-dimensional array in languages that have such can only be done
with an array of arrays. Requiring an array of arrays implies that any
emulation of a multi-dimensional array cannot be done with a single
javascript Array object. I have demonstrated that there is a single case
where an array of arrays can be constructed and still only employ a
single javascript Array object.

You are coming from the wrong assumption that multi-dimensional array
and jagged array are in XOR relations for any given language (so either
m-d or jagged but not both). It is not true: in some languages (say C#)
you can operate with both types. So even if you could create a real m-d
array in JavaScript it would not deny per se the jagged nature of the
Array as it is (thus as it's presented by default in the programming
environment).

I have a feeling that you just don't like the term "jagged" as some
low-class name, like it was with "hash". I could propose than another
as well official naming pair by calling m-d array "safe" array and
jagged array "unsafe" array. I'm not going to use it personally:
because m-d and jagged are more common and more "talking", but for this
thread that could be a compromise.
 
R

Richard Cornford

VK said:
Richard said:
var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}
It is an array of arrays that only employs a single javascript
array.

It is not: you have forgotten that in JavaScript arrays (as
other objects) are passed *by reference*. So you've created
a rather interesting (I have to admit it) circular structure
where on each loop you change the a[] array and add new reference
on it into a[] itself. But by the end you still have nothing but
references to the same object in all elements.

It was you who introduced the expression 'array of arrays' into this
thread, if you don't want to consider an array of elements that have
values that are references to arrays as an array of arrays then you
should not have introduced the expression into this discussion in the
first place.
This is why you cannot make any assignment in
this structure - it would be like to be a spectateur
and the main "hero" on funerals.

It is not the fact that an array or arrays can only be an array of
references to arrays in javascript that precludes meaningful assignment
to array index properties in this structure. That is a consequence of
all of the references referring to the same array, and if they did not
all refer to the same array then this apparently multi-dimensional
structure would not be constructed of a single javascript Array and so
could not be considered as an exception to proposition that the term
multi-dimensional cannot be applied to a single javascript Array.
Same twist can be done with the associative array as well
(with the same useless outcome) so it is not anyhow related
with the array question.

The 'array question' we are dealing with here is whether the term
'jagged' can be applied as a characteristic of javascript Arrays. That
question has side-tracked through a consideration of whether the term
'multi-dimensional' can be applied as a characteristic of javascript
Arrays.

The original question is answered by pure logic because in javascript:-

IF ('jagged') THEN (NOT a single Array object)

- is a truth, and therefor:-

IF (a single Array object) THEN (NOT 'jagged')

- is also a truth. The singleness of any single javascript array
precludes jaggedness so jaggedness is not a characteristic of javascript
arrays (it can only be a characteristic of javascript structures that
would normally be constructed of multiple (at least 2) arrays).

On the other hand:-

IF (multi-dimensional) THEN (NOT a single Array object)

- is not a truth in javascript as a single javascript Array instance can
demonstrably form a structure that resembles the type of structure
needed to implement multi-dimensionalness sufficiently closely to be
regarded as multi-dimensional itself.

It is certainly not a good idea to characterise javascript arrays as
multi-dimensional, but that characterisation cannot be precluded upon
purely logical grounds in the way the characteristic 'jagged' can.
You are coming from the wrong assumption that multi-dimensional
array and jagged array are in XOR relations for any given
language (so either m-d or jagged but not both).

It is hard for me to say what misconceptions of English and logic have
led you to this wrong, irrelevant and insane conclusion. You really are
utterly out of your depth in any reasoned debate.
It is not true: in some languages (say C#) you can operate
with both types. So even if you could create a real m-d array
in JavaScript it would not deny per se the jagged nature of
the Array as it is (thus as it's presented by default in the
programming environment).

It should be so obvious that there is not need to state that for an
array jaggedness pre-supposes multi-dimensionality.
I have a feeling that you just don't like the term "jagged" ...
<snip>

You are wrong.

Richard.
 
T

Thomas 'PointedEars' Lahn

VK said:
Richard said:
var a = [];

for(var c = 0;c < 10;++c){
a[c] = a;
}
It is an array of arrays that only employs a single javascript array.

It is not: you have forgotten that in JavaScript arrays (as other
objects) are passed *by reference*.

Nonsense.

First, it *is* an array of arrays that only employs a single Array (object).

Second, objects are not passed at all, they merely are created and exist
until they are garbage-collected when they are no longer referred.
References to objects are passed to functions by value, just as any other
value. But here no function call is involved, I wonder what this would
have to do with that (well, in fact I do not wonder really -- it is all
too clear that you have exactly no clue what you are talking about).
So you've created a rather interesting (I have to admit it) circular
structure where on each loop you change the a[] array and add new
reference on it into a[] itself. But by the end you still have nothing but
references to the same object in all elements.

Richard recognized that already (but of course you have either not read or
not understood his explanations), and that this is the only way a
J(ava)Script/ECMAScript array can be really multi-dimensional. Which was
the whole point of his example.
This is why you cannot make any assignment in this structure - it would
be like to be a spectateur and the main "hero" on funerals.
^^^^^^^^^^ Should this be French for "spectator"?

More nonsense. An assignment is entirely possible everywhere within this
structure; the reference to the own Array object would be replaced with the
assigned value. The resulting array would be an array where the element
with the same index as the index of the last property access is replaced
with that value. For example, with

a[1][2][3][0] = 42;

/*
the assignment is evaluated as follows:

0. a[1][2][3][0] = 42;
1. a[2][3][0] = 42; // a[1] === a
2. a[3][0] = 42; // a[2] === a
3. a[0] = 42; // a[3] === a

While

42,,,,,,,,,

is displayed with
*/

window.alert(a);

/*
the real, infinitely recursive (`...'), and therefore programmatically
not displayable data structure is

[
42,
[42, [42, [42, [42, [42, [42, ...], ...], ...], ...], ...], ...]
[...],
[...],
[...],
[...],
[...],
[...],
[...],
[...]
]

(where `[...]' is just a means to say "Array object", not "new Array
object"), as can be showed with
*/

window.alert(a[1]); // a[1] === a[2] === ... === a[9] === a
window.alert(a[1][0]); // a[1][0] === a[0] === 42
window.alert(a[1][1]); // a[1][1] === a[1] === a
window.alert(a[1][1][0]); // a[1][1][0] === a[1][0] === a[0] === 42
window.alert(a[1][2][0]); // a[1][2][0] === a[2][0] === a[0] === 42
// ...
window.alert(a[2]); // a[2] === a[1] === ... === a[9] === a
window.alert(a[2][0]); // a[2][0] === a[0] === 42
window.alert(a[2][1][0]); // a[2][1][0] === a[1][0] === a[0] === 42
// ...
window.alert(a[2][2]); // a[2][2] === a[2] === a
window.alert(a[2][2][0]); // a[2][2][0] === a[2][0] === a[0] === 42

// and so on.


PointedEars
 
V

VK

Who's interested in what I'm answering to please read the whole thread
<http://groups.google.com/group/comp...3f60dbb73f0/f3fe79c21622df04#f3fe79c21622df04>


Gentlemen,

That's getting really dangerous for you: in order to prove one wrong
point (that JavaScript Array is not jagged) you had to intriduce a
bounch of new wrong points plus you had to disavow a lot of you
previous statements. It's dangerous because it's a sure way to
transform a discussion into a casuistical match where the only task is
to negate the very last statement of your opponent without relation
with the topic and the previous statements.

In your attempt to dismiss VK you even stated that JavaScript Array is
a whole separate data structure having much more difference from other
structures (like generic Object) than simply length autocounter. At any
other time it would fill my heart with joy, but not now because I see
that it is not an expression of the real believe I'm seeking from you
but a momentary side-effect of your excitement :)

Questions to be answered:
1) What is multi-dimensional array
2) What is jagged array
3) What is safe and unsafe array and their relations with m-d and j
arrays
4) What is the nature of JavaScript Array

I know the exact answers on each and every of these questions after one
year of careful studies. I still want to give each question not by my
stupid mouth but by some *reputable* *printed* source. It will take
some time (within a week I guess) as I'm mostly on my JSONet project at
my spare time. But I feel it is necessary to move out the discussion
out of the personal opinions into a discussion over some publically
established categories.

I guess it will be more productive.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Should not that have rather been a "Yes it is because ..." answer? ;-)

I guess the joke could be that it's not possible to operate on an
array without know how many elements it has, because you *do* know how
many elements it has.

(and then we are back at the discussion about whether a unassigned
index less than length counts as an element or not :)
/L
 
L

Lasse Reichstein Nielsen

VK said:
Questions to be answered:
1) What is multi-dimensional array

It's a vague term. Let's use the C definition, where it is a
rectangular structure of elements, that requires more than one index
to look up the elements. The structure is "rectangular" in that the
range of valid indices on the different index-axes are independent of
eachother.
2) What is jagged array

I must guess here, because I haven't heard it before. I'm guessing
that it is a structure where you need more than one index to look up
elements, but where the range of valid values on the secondary (and
later) æs depend on the previous ones.

This is what you get with an array of array references in most
langauges, including C (where references are called pointers), Java
and Javascript.
3) What is safe and unsafe array and their relations with m-d and j
arrays

Have no idea. What are safe and unsafe arrays?
4) What is the nature of JavaScript Array

Ah, the old question.

A Javascript array is a special Javascript host object, possibly
created using the Array constructor function, where certain property
names are considered special "array indices", and which has a length
property that is always at least one larger than the largest "array
index" property that the object contains.

This allows you to treat the array object as a sparse array in other
languages, while still using all object properties on it as well.



/L
 
R

Richard Cornford

VK said:
Who's interested in what I'm answering to please read the
whole thread
http://groups.google.com/...

What is the point of referring people who are reading this thread to
this thread?
Gentlemen,

That's getting really dangerous for you:

Dangerous? The only danger is of wasting ones time as your intellectual
arsenal is so deficient that you may never see that the point under
discussion was resolved at the time Randy posted yesterday.
in order to prove one wrong point (that JavaScript Array is
not jagged)

Not only is that not one wrong point, it is a point that is so not wrong
that it can be proven logically. The only potential vulnerability of
that proof being someone being able to demonstrate a structure
exhibiting 'jaggedness' created with a single javascript Array instance
(and that looks like an imposibility).
you had to intriduce a bounch of new wrong points

State one. (Or if it is a bunch state all of them, though attempting to
state one will be sufficient to show that no such 'new wrong points'
have been introduced.).

In fact the ability to assert that:-

IF ('jagged') THEN (NOT a single Array object)

- is a truth in javascript is all that is necessary to prove that the
term 'jagged' cannot characterise a javascript Array (only, possibly,
some javascript structures).
plus you had to disavow a lot of you previous statements.

State one. (Or if it is a lot state all of them, though attempting to
state one will be sufficient to show that no previous statements have
been disavowed).
It's dangerous because it's a sure way to transform a discussion
into a casuistical match where the only task is to negate the
very last statement of your opponent without relation
with the topic and the previous statements.

Your poor attempt to apply my "own thesis" to C++ and Java arrays
demonstrated the only likely source of flawed reasoning in this
discussion.
In your attempt to dismiss VK you even stated that JavaScript
Array is a whole separate data structure having much more
difference from other structures (like generic Object) than
simply length autocounter.

Where? If that has been said you should be able to quote the specific
statment.
At any other time it would fill my heart with joy, but not
now because I see that it is not an expression of the real
believe I'm seeking from you but a momentary side-effect of your
excitement :)

More likely you perception of such a statement where none exists is
merely an artefact of your wider misconceptions, incomprehension of
English and general irrationality.
Questions to be answered:
1) What is multi-dimensional array

Something that does not exist in javascript, but can be emulation with a
structure (most directly with an array of arrays).
2) What is jagged array

Something that does not exist in javascript, but can be emulation with a
structure (most directly with an array of arrays).
3) What is safe and unsafe array and their relations with
m-d and j arrays

Irrelevant notions that popped out of your mind for no apparent reason,
and with no real purpose.
4) What is the nature of JavaScript Array

Whatever else the nature of a javascript Array is it is certainly not in
its nature to be 'jagged'.
I know the exact answers on each and every of these
questions after one year of careful studies.

LOL. When you announced last week that one month of studying Mozilla
browsers had taken you from a position of (presumably) not knowing
whether it supports the document.styleSheets collection to concluding
that it does not you made it obvious that for you study actually
increases the falseness of your beliefs. If a month's application of
your perverse mental processes can do that I hate to imagine how wrong
you could be after a year.

It should be obvious that you cannot be the only person studying this
subject and so when you are the _only_ person studying the subject who
ends up believing what your believe about it (and you cannot convince
anyone with any familiarity with the subject that you are correct) it
should be equally obvious that your you are applying a flawed process to
your studies.
I still want to give each question not by my stupid mouth but
by some *reputable* *printed* source.

Very few sources of information about javascript are both reputable and
printed.
It will take some time (within a week I guess) as I'm
mostly on my JSONet project at my spare time.

I won't be holding my breath. So far whenever you have promised that you
would post some response at a more distant future date you have not done
so (and I was looking forward to you attempting to defend your
misrepresentation of Ockham's razor, but it has not materialised yet).
But I feel it is necessary to move out the discussion out
of the personal opinions into a discussion over some
publically established categories.

The only unsustainable personal opinions being post to this thread have
been posted by you, so if you want to keep the discussion within the
area of reasoned discussion that is entirely at your discretion (though
potentially not within your power).

You should remember that we are on Usenet here. If I were posting what
was perceived as irrational nonsense there would be people posting
follow-ups telling me so. That doesn't appear to be happening, so what
is the best explanation for that?
I guess it will be more productive.

What I have seen of your "JSONet project" so far falls into the category
of "bizarre and obtuse" so no, it probably won't be more productive
(unless you would otherwise spend the time 'studying'). On the other
hand it probably won't be less productive for you either.

Richard.
 
R

Randy Webb

Jasen Betts said the following on 2/3/2006 4:48 AM:
No it is not because all arrays have a length property which tells you how
many elements they have :)

You sure about that?

<script type="text/javascript">
var myArray = new Array()
myArray['myArrayElement'] = 'something'
alert(myArray.length)
</script>
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top