473,574 Members | 3,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Feb 2 '06 #1
104 16897
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
Feb 2 '06 #2
VK

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.c om/schools_ring/ArrayAndHash.ht ml>
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.

Feb 2 '06 #3
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/
Feb 2 '06 #4
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.c om/schools_ring/ArrayAndHash.ht ml>


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.c om/groups?as_q=Arr ay&as_ugroup=co mp.lang.javascr ipt&scoring=d&f ilter=0>
<URL:http://groups.google.c om/groups?as_uauth ors=VK&as_ugrou p=comp.lang.jav ascript&scoring =d&filter=0>
PointedEars
Feb 2 '06 #5
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><ti tle>Little Sum Function</title></head>
<body><script type="text/javascript">
function s(a){return Function('retur n '+a.join('+'))( )}
document.write( s([1,654,2,5,489,5 1,3851,681,32,5 ,0]))
</script></body></html>

Feb 2 '06 #6
VK

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.c om/schools_ring/ArrayAndHash.ht ml> 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)

Feb 2 '06 #7
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>Li ttle Sum Function</title></head>
<body><script type="text/javascript">
function s(a){return Function('retur n '+a.join('+'))( )}
document.write( s([1,654,2,5,489,5 1,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"><he ad><meta
http-equiv="Content-Type" content="text/html;charset=IS O-8859-1">
<title>Little Adder</title></head><script
type="text/javascript">doc ument.write(Fun ction("return "
+[1,654,2,5,489,5 1,3851,681,32,5 ,0].join("+"))()); </script>
Regards,
PointedEars
___________
[1] especially because there is no evil[tm] eval()
Feb 2 '06 #8
Jambalaya wrote on 02 feb 2006 in comp.lang.javas cript:
This is the smallest example I could write:

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


<script type="text/javascript">
alert(eval([1,654,2,5,489,5 1,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,5 1,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)
Feb 2 '06 #9
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
Feb 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
55541
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are also removed) So far I have (val is value, ar is array, returns new array):
13
2501
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
5
6510
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with the object references. However, I just realized that indices may only be referenced by strings.
47
5052
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
22
4590
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and...
2
5211
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it down to a very simple test case which can be cut-n-pasted into a .html file and viewed in a browser: ...
21
3186
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
30
2924
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
2
2494
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
0
7843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8140
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6514
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3776
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.