Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > adding elements to an array...

Reply
Thread Tools

adding elements to an array...

 
 
Frances
Guest
Posts: n/a
 
      05-30-2006
how to do you add elements to an array? am looking at section dealing
with arrays in JS Bible, can't find how you add to an array.. in the
last few years I've been doing much more Java than JS, am used to Java,
where you can't add elements to an array, but use a Vector instead.. and
it's very easy to add to it (use add() method..) how to you add
elements to an array in JavaScript..

I looked up array obj, don't see method to add to array...

thank you..
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-30-2006


Frances wrote:

> how to do you add elements to an array?


There is a push method that adds its arguments to the end of the array e.g.
var a = [0, 1, 2];
a.push(3);
Note that push is not supported by IE 5/Win with the JScript 5.0 engine.
You can always do e.g.
a[a.length] = 3;

There are other methods to change the array, see
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array #Mutator_methods>


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      05-30-2006
On 30/05/2006 20:59, Vic Sowers wrote:

[snip]

> a[-1] = "Zero";
> a["Zero"] = -1;


Neither of these will be array elements.

What constitutes an array index has been described several times (both
by myself and others).

See, for example:

Author: Michael Winter
Date: 2004-12-07 10:26:36
Message-ID: opsimviozvx13kvk@atlantis

<http://groups.google.co.uk/group/comp.lang.javascript/msg/a7bf8971b33f971e>

in the thread, "Roundup of FAQ change requests".

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
 
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
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
How do I allow both elements or No elements Billy XML 4 09-12-2005 08:29 AM
Elements within elements Jyrki Keisala XML 5 06-15-2005 04:58 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 1 01-30-2004 04:09 PM
container elements for repeating elements ('element farms') needed? Wolfgang Lipp XML 0 01-28-2004 02:50 PM



Advertisments