Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Array in javascript

 
 
Leszek
Guest
Posts: n/a
 
      02-02-2006
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


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

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.

 
Reply With Quote
 
Guillaume
Guest
Posts: n/a
 
      02-02-2006
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-02-2006
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
 
Reply With Quote
 
Jambalaya
Guest
Posts: n/a
 
      02-02-2006
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>

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

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)

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-02-2006
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()
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-02-2006
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)
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      02-02-2006
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
 
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