JavaScript Array Methods
- Previous Page JS Arrays
- Next Page JS Array Search
The power of JavaScript arrays is hidden in array methods.
Convert an array to a string
JavaScript method toString()
Convert an array to a string of array values (comma-separated).
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString();
Result
Banana,Orange,Apple,Mango
join()
The method can also combine all array elements into a single string.
It behaves like toString(), but you can also specify a delimiter:
Example
var fruits = ["Banana", "Orange","Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.join(" * ");
Result
Banana * Orange * Apple * Mango
Popping and Pushing
It is very simple to delete elements and add new elements when processing arrays.
Popping and Pushing refer to:
From the arrayPoppingItem, or to add to the arrayPushingItem.
Popping
pop()
The method removes the last element from the array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); // Remove the last element ("Mango") from fruits
pop()
The method returns the value that was "popped":
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.pop(); // x's value is "Mango"
Pushing
push()
The method (at the end of the array) adds a new element to the array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi"); // Add a new element to fruits
push()
The method returns the new length of the array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.push("Kiwi"); // x's value is 5
Shifting elements
Shifting is equivalent to popping, but it deals with the first element instead of the last.
shift()
The method removes the first array element and shifts all other elements to a lower index.
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); // Remove the first element "Banana" from fruits
shift()
方法返回被“位移出”的字符串:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; The method returns the string that has been "displaced out":
fruits.unshift("Lemon"); // Add a new element "Lemon" to fruits
fruits.shift(); // Returns "Banana"
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; method (at the beginning) adds a new element to the array and "reversely displaces" the old elements:
fruits.unshift("Lemon"); // Add a new element "Lemon" to fruits
unshift()
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; The method returns the length of the new array.
fruits.unshift("Lemon"); // Returns 5
change elementsBy using theirindices
to access array elements:arrayindices (subscripts)
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; Starting with 0. [0] is the first array element, [1] is the second, [2] is the third ...
fruits[0] = "Kiwi"; // Change the first element of fruits to "Kiwi"
length
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; The property provides an easy way to append new elements to the array:
fruits[fruits.length] = "Kiwi"; // Append "Kiwi" to fruits
delete elements delete
Since JavaScript arrays are objects, their elements can be accessed using JavaScriptoperator to:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits[0]; // Change the first element of fruits to undefined
Use delete
will leave undefined holes in the array. Please use pop()
or shift()
instead.
Concatenate arrays
splice()
This method can be used to add new items to the array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 0, "Lemon", "Kiwi");
The first parameter (2) defines the position (concatenation) where the new element should be added.
The second parameter (0) defines how many elements should be deleted.
The remaining parameters ("Lemon", "Kiwi") define the new elements to be added.
splice()
The method returns an array containing the deleted items:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 2, "Lemon", "Kiwi");
Use splice() to delete elements
With clever parameter settings, you can use splice()
Remove elements without leaving 'holes' in the array:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(0, 1); // Delete the first element from the fruits array
The first parameter (0) defines where the new element should be inserted.Add(be inserted).
The second parameter (1) defines the position where it shouldDelete multipleelements.
The remaining parameters are omitted. No new elements will be added.
Merge (connect) arrays
concat()
The method creates a new array by merging (connecting) existing arrays:
Example (merging two arrays)
var myGirls = ["Cecilie", "Lone"]; var myBoys = ["Emil", "Tobias", "Linus"]; var myChildren = myGirls.concat(myBoys); // Connect myGirls and myBoys
concat()
The method does not change the existing array. It always returns a new array.
concat()
The method can use any number of array parameters:
Example (merging three arrays)
var arr1 = ["Cecilie", "Lone"]; var arr2 = ["Emil", "Tobias", "Linus"]; var arr3 = ["Robin", "Morgan"]; var myChildren = arr1.concat(arr2, arr3); // Connect arr1, arr2 and arr3 together
concat()
The method can also take values as parameters:
Example (merging an array with values)
var arr1 = ["Cecilie", "Lone"]; var myChildren = arr1.concat(["Emil", "Tobias", "Linus"]);
Trim the array
slice()
The method creates a new array using a segment of the array.
In this example, it cuts out a segment of the array starting from array element 1 ("Orange"):
Example
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1);
slice()
The method creates a new array. It does not remove any elements from the source array.
In this example, it cuts out a segment of the array starting from array element 3 ("Apple"):
Example
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(3);
slice()
It can accept two parameters, such as (1, 3).
This method selects elements from the start parameter to the end parameter (excluding).
Example
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1, 3);
If the end parameter is omitted, for example in the first example, then slice()
It will cut out the remaining part of the array.
Example
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(2);
Automatic toString()
If you need the original value, JavaScript will automatically convert the array to a string. The following two examples will produce the same result:
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString();
Example
var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits;
All JavaScript objects have toString()
Methods.
Array Sorting
We will learn about array sorting in the next chapter ~.
Find the Maximum and Minimum Values in an Array
There are no built-in functions in JavaScript arrays to find the highest and lowest values.
You will learn how to solve this problem in the next chapter of this tutorial.
Complete Array Reference Manual
For a complete reference manual, please visit our full JavaScript Array Reference Manual.
This reference manual includes descriptions and examples of all array properties and methods.
- Previous Page JS Arrays
- Next Page JS Array Search