Is there a javascript arraylist?

I have a bunch of things that I want to add to the array, and I don't know what size the array will be in advance. Can I do something similar to C # arraylist in javascript and repeat myArray.Add(object); On him?

+63
javascript
Nov 17 '09 at 13:15
source share
9 answers

just use array.push ();

 var array = []; array.push(value); 

This will add another element to it.

To make one, use array.pop();

Link to JavaScript arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

+105
Nov 17 '09 at 13:16
source share

There is no ArrayList in javascript.

However, there is Array ECMA 5.1 , which has similar functionality for the "ArrayList". Most of this answer is taken verbatim from the HTML rendering of Ecma-262 Edition 5.1, ECMAScript Language Specification .

Certain arrays have the following methods available:

  • .toString ( )
  • .toLocaleString ( )
  • .concat ( [ item1 [ , item2 [ , … ] ] ] )
    When the concat method is called with zero or more arguments item1, item2, etc., it returns an array containing the elements of the array from the object, followed by the elements of the array of each argument in order.
  • .join (separator)
    The elements of the array are converted to strings, and these strings are then merged, separated by occurrences of the separator. If no delimiter is provided, one comma is used as the delimiter.
  • .pop ( )
    The last element of the array is removed from the array and returned.
  • .push ( [ item1 [ , item2 [ , … ] ] ] )
    Arguments are added to the end of the array in the order in which they are displayed. The new array length is returned as a result of the call. "
  • .reverse ( )
    Array elements are rearranged to reverse the order. The object is returned as a result of the call.
  • .shift ( )
    The first element of the array is removed from the array and returned. "
  • .slice (start, end)
    The slice method takes two arguments: start and end, and returns an array containing the elements of the array, starting from the element, starting at, but not including the end of the element (or through the end of the array if the end is undefined).
  • .sort (comparefn)
    Elements of this array are sorted. A variety is not necessarily stable (i.e. Elements that compare equals do not necessarily remain in their original order). If comparefn is not undefined, it must be a function that takes two arguments x and y and returns a negative value if x <y, zero if x = y, or a positive value if x> y.
  • .splice (start, deleteCount [ , item1 [ , item2 [ , … ] ] ] )
    When the splice method is called with two or more arguments start, deleteCount and (optionally) item1, item2, etc., deleteCount the elements of the array, starting at the beginning of the index of the array, are replaced by the arguments item1, item2, etc. An Array object is returned containing the deleted items (if any).
  • .unshift ( [ item1 [ , item2 [ , … ] ] ] )
    Arguments are added to the beginning of the array, so that their order in the array matches the order in which they appear in the argument list.
  • .indexOf ( searchElement [ , fromIndex ] )
    indexOf compares searchElement with array elements in ascending order using the strict equality comparison algorithm (11.9.6), and if it is found in one or more positions, returns the index of the first such position; otherwise -1 is returned.
  • .lastIndexOf ( searchElement [ , fromIndex ] )
    lastIndexOf compares searchElement with array elements in descending order using the strict equality internal comparison algorithm (11.9.6) and if found in one or more positions returns the index of the last such position; otherwise -1 is returned.
  • .every ( callbackfn [ , thisArg ] )
    callbackfn must be a function that takes three arguments and returns a value that is mandatory for a boolean value of true or false. each callbackfn call once for each element present in the array, in ascending order, until it finds one where callbackfn returns false. If such an element is found, each immediately returns false. Otherwise, if callbackfn returns true for all elements, each returns true.
  • .some ( callbackfn [ , thisArg ] )
    callbackfn must be a function that takes three arguments and returns a value that is mandatory for a boolean value of true or false. some callbackfn calls once for each element present in the array, in ascending order, until it finds one where callbackfn returns true. If such an element is found, some immediately return true. Otherwise, some return false.
  • .forEach ( callbackfn [ , thisArg ] )
    callbackfn must be a function that takes three arguments. forEach calls callbackfn once for each element present in the array, in ascending order.
  • .map ( callbackfn [ , thisArg ] )
    callbackfn must be a function that takes three arguments. map calls callbackfn once for each element in the array, in ascending order, and builds a new array from the results.
  • .filter ( callbackfn [ , thisArg ] )
    callbackfn must be a function that takes three arguments and returns a value that is mandatory for a boolean value of true or false. the filter calls callbackfn once for each element of the array in ascending order and creates a new array from all values ​​for which callbackfn returns true.
  • .reduce ( callbackfn [ , initialValue ] )
    callbackfn must be a function that takes four arguments. reduces callback calls as a function, once for each element present in the array, in ascending order.
  • .reduceRight ( callbackfn [ , initialValue ] )
    callbackfn must be a function that takes four arguments. reduceRight calls the callback as a function once for each element present in the array, in descending order.

as well as the length property.

+41
Dec 16 '13 at 22:48
source share

With javascript, all arrays are flexible. You can simply do something like the following:

 var myArray = []; myArray.push(object); myArray.push(anotherObject); // ... 
+22
Nov 17 '09 at 13:17
source share

Arrays are pretty flexible in JS, you can do:

 var myArray = new Array(); myArray.push("string 1"); myArray.push("string 2"); 
+14
Nov 17 '09 at 13:16
source share

javascript uses dynamic arrays, no need to declare size in advance

you can click and move into arrays as many times as you want, javascript will handle the distribution and stuff for you

+5
Nov 17 '09 at 13:16
source share

You don’t even need a push, you can do something like this -

 var A=[10,20,30,40]; A[A.length]=50; 
+5
Nov 17 '09 at 14:54
source share

In a Java script, you declare an array as shown below:

 var array=[]; array.push(); 

and for arraylist or object or array you should use json; and serialize it using json using the following code:

  var serializedMyObj = JSON.stringify(myObj); 
+3
Dec 20 '13 at 15:26
source share

Use the javascript array push() method, it adds this object to the end of the array. JS arrays are quite flexible, you can click as many objects as you want in the array without specifying its length in advance. In addition, different types of objects can be transferred to the same array.

+1
Dec 21 '13 at 23:14
source share

Just use array.push(something); . In this regard, Javascript arrays are similar to ArrayLists - they can be viewed as they have a flexible length (unlike java arrays).

0
Dec 23 '13 at 14:40
source share



All Articles