Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Array in javascript

Reply
Thread Tools

Array in javascript

 
 
RobG
Guest
Posts: n/a
 
      02-03-2006
VK wrote:
> Ian Collins wrote:
>
>>I think the previous responses over looked the use of
>>for( var n in array ) {} as a method for iterating over an array.
>>
>>
>>>Could you show me a little example how to do this?
>>>

>>
>>function test( names )
>>{
>> for( var name in names )
>> {
>> // do something.
>> }
>>}

>
>
> 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[i]);
> }
>
> 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.


--
Rob
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      02-03-2006
RobG said the following on 2/3/2006 9:11 AM:
> VK wrote:
>> Ian Collins wrote:
>>
>>> I think the previous responses over looked the use of
>>> for( var n in array ) {} as a method for iterating over an array.
>>>
>>>
>>>> Could you show me a little example how to do this?
>>>>
>>>
>>> function test( names )
>>> {
>>> for( var name in names )
>>> {
>>> // do something.
>>> }
>>> }

>>
>>
>> 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.


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
 
 
 
John W. Kennedy
Guest
Posts: n/a
 
      02-03-2006
Duncan Booth wrote:
> RobG wrote:
>
>>> 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.
>>

>
> .... 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"
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      02-03-2006
Richard Cornford wrote:
<snip>
> 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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-03-2006
John W. Kennedy wrote:

> 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.


PointedEars
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-03-2006

Randy Webb wrote:
> 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?

 
Reply With Quote
 
John W. Kennedy
Guest
Posts: n/a
 
      02-03-2006
Thomas 'PointedEars' Lahn wrote:
> John W. Kennedy wrote:
>
>> 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.

--
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"
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-03-2006
John W. Kennedy wrote:

> Thomas 'PointedEars' Lahn wrote:
>> John W. Kennedy wrote:
>>> 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-03-2006
Jasen Betts wrote:

> On 2006-02-02, Leszek <(E-Mail Removed)> wrote:
>> Is it possible in javascript to operate on an array without knowing how
>> mamy elements it has?

>
> 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
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-03-2006

Richard Cornford wrote:
> Richard Cornford wrote:
> <snip>
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Array function SPLICE doesn't work with Select->OPTIONS array. JavaScript BrianP Javascript 2 09-18-2005 04:20 AM
Array and Hash (Associative array) in JavaScript v.3.0 VK Javascript 36 08-03-2005 02:39 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments