Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Array in javascript (http://www.velocityreviews.com/forums/t922765-array-in-javascript.html)

Leszek 02-02-2006 08:07 PM

Array in javascript
 
Hi.

Is it possible in javascript to operate on an array without knowing how mamy
elements it has?
What i want to do is sending an array to a script, and this script should
add all values from that array

Could you show me a little example how to do this?

Thanks.
Leszek



Thomas 'PointedEars' Lahn 02-02-2006 08:36 PM

Re: Array in javascript
 
Leszek wrote:

> Is it possible in javascript to operate on an array without knowing how
> mamy elements it has?


Of course. This is what the e.g. `length' property of Array objects is for.

> What i want to do is sending an array to a script, and this
> script should add all values from that array
>
> Could you show me a little example how to do this?


Depends on what you mean by "sending an array to a script" (which array,
which script, client-side or server-side, send from where to where?) and by
"script should add all values from that array" (add from where exactly to
which?)


PointedEars

VK 02-02-2006 09:17 PM

Re: Array in javascript
 

Leszek wrote:
> Hi.
>
> Is it possible in javascript to operate on an array without knowing how mamy
> elements it has?


JavaScript array is Dynamic Sparse Jagged Array. That can be too much
of adjectives for one time :-) but the first one means that can add new
elements to array without ReDim / resize() it.

var myArray = new Array(); // 0 elements
myArray[0] = 0;
....
....
myArray[1000] = 1000;

About other adjectives (and about JavaScript arrays at whole) you can
read at:
<http://www.geocities.com/schools_ring/ArrayAndHash.html>

> What i want to do is sending an array to a script, and this script should
> add all values from that array
> Could you show me a little example how to do this?


As it was pointed out it depends on how and in what form are you
getting the original data for your array.


Guillaume 02-02-2006 09:47 PM

Re: Array in javascript
 
Leszek
> Is it possible in javascript to operate on an array without knowing how mamy
> elements it has?


I don't think, there is no function like 'map' or 'apply'. However, they
are not difficult to implement because you always know the size of an
array: array.length;

--
My desktop is worth a million of dollars. Put an icon on it.
http://www.milliondollarscreenshot.com/

Thomas 'PointedEars' Lahn 02-02-2006 09:49 PM

Re: Array in javascript
 
VK wrote:

> Leszek wrote:
>> Is it possible in javascript to operate on an array without knowing how
>> mamy elements it has?

>
> JavaScript array is Dynamic Sparse Jagged Array. That can be too much
> of adjectives for one time :-)


But I am sure you can invent more just to cover your lack of knowledge and
understanding of the basics.

> but the first one means that can add new
> elements to array without ReDim / resize() it.
>
> var myArray = new Array(); // 0 elements
> myArray[0] = 0;
> ...
> ...
> myArray[1000] = 1000;


You did not answer the question of the OP _at all_. Figures.

> About other adjectives (and about JavaScript arrays at whole) you can
> read at:
> <http://www.geocities.com/schools_ring/ArrayAndHash.html>


This text contains a lot of, if not consists mainly of, factually incorrect
information, presented as being the truth despite of that. Readers are
strongly recommended to ignore it, to handle all statements of its author
regarding (proper) software development with extreme care, and to read
previous discussions on the subject instead.

<URL:http://groups.google.com/groups?as_q=Array&as_ugroup=comp.lang.javascript&s coring=d&filter=0>
<URL:http://groups.google.com/groups?as_uauthors=VK&as_ugroup=comp.lang.javascri pt&scoring=d&filter=0>


PointedEars

Jambalaya 02-02-2006 09:58 PM

Re: Array in javascript
 
Leszek wrote:
> Hi.
>
> Is it possible in javascript to operate on an array without knowing how mamy
> elements it has?


Yes.

> What i want to do is sending an array to a script, and this script should
> add all values from that array
>
> Could you show me a little example how to do this?


This is the smallest example I could write:

<html><head><title>Little Sum Function</title></head>
<body><script type="text/javascript">
function s(a){return Function('return '+a.join('+'))()}
document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
</script></body></html>


VK 02-02-2006 10:13 PM

Re: Array in javascript
 

Thomas 'PointedEars' Lahn wrote:
> VK wrote:
>
> > Leszek wrote:
> >> Is it possible in javascript to operate on an array without knowing how
> >> mamy elements it has?

> >
> > JavaScript array is Dynamic Sparse Jagged Array. That can be too much
> > of adjectives for one time :-)

>
> But I am sure you can invent more just to cover your lack of knowledge and
> understanding of the basics.


JavaScript array is Dinamic, is Sparse and is Jagged. These three core
features are important to know to operate properly with arrays and get
expected results.

<http://www.geocities.com/schools_ring/ArrayAndHash.html> is the only
one known (to me at least) resource there all three features along with
other information would be named and illustrated properly: all within
the same page. Unfortunately (after careful reading and searching) this
is still the only resource I can endorse for JavaScript array
information.

It should be updated though by removing some no so relevant part at the
bottom and by adding new array methods from JavaScript 1.6 (Firefox 1.5)


Thomas 'PointedEars' Lahn 02-02-2006 10:44 PM

Re: Array in javascript
 
Jambalaya wrote:

> Leszek wrote:
>> What i want to do is sending an array to a script, and
>> this script should add all values from that array
>>
>> Could you show me a little example how to do this?

>
> This is the smallest example I could write:
>
> <html>
> <head><title>Little Sum Function</title></head>
> <body><script type="text/javascript">
> function s(a){return Function('return '+a.join('+'))()}
> document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
> </script></body></html>


Nice[1] :)

Not shorter, but Valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><head><meta
http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Little Adder</title></head><script
type="text/javascript">document.write(Function("return "
+[1,654,2,5,489,51,3851,681,32,5,0].join("+"))());</script>


Regards,
PointedEars
___________
[1] especially because there is no evil[tm] eval()

Evertjan. 02-02-2006 10:53 PM

Re: Array in javascript
 
Jambalaya wrote on 02 feb 2006 in comp.lang.javascript:

> This is the smallest example I could write:
>
> <html><head><title>Little Sum Function</title></head>
> <body><script type="text/javascript">
> function s(a){return Function('return '+a.join('+'))()}
> document.write(s([1,654,2,5,489,51,3851,681,32,5,0]))
> </script></body></html>
>


<script type="text/javascript">
alert(eval([1,654,2,5,489,51,3851,681,32,5,0].join('+')));
</script>

or for the (eval==evil)-ers:

<script type="text/javascript">
var b=0,a=[1,654,2,5,489,51,3851,681,32,5,0],z=a.length;
while(z)b+=a[--z];alert(b)
</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

RobG 02-02-2006 11:13 PM

Re: Array in javascript
 
Leszek wrote:
[...]
> What i want to do is sending an array to a script, and this script should
> add all values from that array
>
> Could you show me a little example how to do this?


// An array of numbers, some as strings
var A = [2,'5',8,'2',4];

// A function that adds all the elements of an array
function addEm(X)
{
var i = X.length;
var sum = 0;
while(i--) sum += +X[i];
return sum;
}

// Show it in action
alert( addEm(A) );


If there is any chance that some of the elements might not be numbers,
then that should be checked before attempting addition:

var A = [2,'blah','5',8,2,4];

function addEm(X)
{
var x, i = X.length;
var sum = 0;
while(i--) {
x = +X[i];
if ( !isNaN(x) ) sum += x;
}
return sum;
}



--
Rob


All times are GMT. The time now is 12:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.