Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Javascript Array

  Asked By: Tracey    Date: Apr 11    Category: Java    Views: 1036
  

In Java we have Vector. And we can add elements in vector using
addElements. Now, I got an Array in JavaScript and I want to add
elements in javascript as well. I created new array like this
var newArray = new Array();
// here I want to add elements dynamically,

Is it Possible ?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Anna Hill     Answered On: Apr 11

As you probably know, javascript  handles objects in odd ways compared
to java  and other languages. It is possible to have an array  grow
dynamically in Javascript. Here is a code snippit:

<script type="text/javascript" language="JavaScript">
<!-- Hide this script from incompatible browsers!

var stuff = new Array(3)
stuff[1] = "hello"
stuff[2] = 27
stuff[3] = 8.5
stuff[5] = "element number 5"
for( i = 1; i <= 5; i++){
document.writeln("stuff[" + i + "] = " + stuff[i]);
}
-->
------------------------------------------------

Here is the output:

stuff[1] = hello
stuff[2] = 27
stuff[3] = 8.5
stuff[4] = undefined
stuff[5] = element number 5

-------------------------------------------------

Notice that while we define the array as having indices 1 to 3, we
extend it by assigning a value to element number 5.

 
Answer #2    Answered By: Alexander Fields     Answered On: Apr 11

But, my question is this.

var stuff = new Array() ;
In your message you said
stuff[1] = "string"
stuff[2] = "string2"

But, my question is how would I increse the Index value ( stuff[] ).
The new value I get that can be set like this

stuff[] = "newvalue' // but again how would I or what shold I put in
stuff[]. I hope you understand my question..

 
Didn't find what you were looking for? Find more on Javascript Array Or get search suggestion and latest updates.




Tagged: