[javascript] Is there an arraylist in Javascript?

I have a bunch of things I want to add into an array, and I don't know what the size of the array will be beforehand. Can I do something similar to the c# arraylist in javascript, and do myArray.Add(object); repeatedly on it?

This question is related to javascript

The answer is


just use array.push();

var array = [];


array.push(value);

This will add another item to it.

To take one off, use array.pop();

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


In Java script you declare array as below:

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

and for arraylist or object or array you have to use json; and Serialize it using json by using following code:

 var serializedMyObj = JSON.stringify(myObj);

Use javascript array push() method, it adds the given object in the end of the array. JS Arrays are pretty flexible,you can push as many objects as you wish in an array without specifying its length beforehand. Also,different types of objects can be pushed to the same Array.


Arrays are pretty flexible in JS, you can do:

var myArray = new Array();
myArray.push("string 1");
myArray.push("string 2");

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

var myArray = [];

myArray.push(object);
myArray.push(anotherObject);
// ...

Just use array.push(something);. Javascript arrays are like ArrayLists in this respect - they can be treated like they have a flexible length (unlike java arrays).


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

var A=[10,20,30,40];

A[A.length]=50;

There is no ArrayList in javascript.

There is however ArrayECMA 5.1 which has similar functionality to an "ArrayList". The majority of this answer is taken verbatim from the HTML rendering of Ecma-262 Edition 5.1, The ECMAScript Language Specification.

Defined 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 array elements of the object followed by the array elements of each argument in order.
  • .join (separator)
    The elements of the array are converted to Strings, and these Strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.
  • .pop ( )
    The last element of the array is removed from the array and returned.
  • .push ( [ item1 [ , item2 [ , … ] ] ] )
    The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call."
  • .reverse ( )
    The elements of the array are rearranged so as to reverse their order. The object is returned as the 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 from element start up to, but not including, element end (or through the end of the array if end is undefined).
  • .sort (comparefn)
    The elements of this array are sorted. The sort is not necessarily stable (that is, elements that compare equal do not necessarily remain in their original order). If comparefn is not undefined, it should be a function that accepts 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., the deleteCount elements of the array starting at array index start are replaced by the arguments item1, item2, etc. An Array object containing the deleted elements (if any) is returned.
  • .unshift ( [ item1 [ , item2 [ , … ] ] ] )
    The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list.
  • .indexOf ( searchElement [ , fromIndex ] )
    indexOf compares searchElement to the elements of the array, in ascending order, using the internal Strict Equality Comparison Algorithm (11.9.6), and if found at one or more positions, returns the index of the first such position; otherwise, -1 is returned.
  • .lastIndexOf ( searchElement [ , fromIndex ] )
    lastIndexOf compares searchElement to the elements of the array in descending order using the internal Strict Equality Comparison Algorithm (11.9.6), and if found at one or more positions, returns the index of the last such position; otherwise, -1 is returned.
  • .every ( callbackfn [ , thisArg ] )
    callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. every calls callbackfn 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, every immediately returns false. Otherwise, if callbackfn returned true for all elements, every will return true.
  • .some ( callbackfn [ , thisArg ] )
    callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. some calls callbackfn 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 returns true. Otherwise, some returns false.
  • .forEach ( callbackfn [ , thisArg ] )
    callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order.
  • .map ( callbackfn [ , thisArg ] )
    callbackfn should be a function that accepts three arguments. map calls callbackfn once for each element in the array, in ascending order, and constructs a new Array from the results.
  • .filter ( callbackfn [ , thisArg ] )
    callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. filter calls callbackfn once for each element in the array, in ascending order, and constructs a new array of all the values for which callbackfn returns true.
  • .reduce ( callbackfn [ , initialValue ] )
    callbackfn should be a function that takes four arguments. reduce calls the callback, as a function, once for each element present in the array, in ascending order.
  • .reduceRight ( callbackfn [ , initialValue ] )
    callbackfn should 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.

and also the length property.


Try this, maybe can help, it do what you want:

ListArray

_x000D_
_x000D_
var listArray = new ListArray();_x000D_
let element = {name: 'Edy', age: 27, country: "Brazil"};_x000D_
let element2 = {name: 'Marcus', age: 27, country: "Brazil"};_x000D_
listArray.push(element);_x000D_
listArray.push(element2);_x000D_
_x000D_
console.log(listArray.array)
_x000D_
<script src="https://marcusvi200.github.io/list-array/script/ListArray.js"></script>
_x000D_
_x000D_
_x000D_


javascript uses dynamic arrays, no need to declare the size beforehand

you can push and shift to arrays as many times as you want, javascript will handle allocation and stuff for you