Array object
1. Method of not changing the original array
concat()
The concat() method is used to join two or more arrays.
Parameter: required. This parameter can be a concrete value or an array object. It can be any number.
Return value: returns the connected new array
var hege = ["Cecilie", "Lone"]; var stale = ["Emil", "Tobias", "Linus"]; var kai = ["Robin"]; var children = hege.concat(stale,kai); result: Cecilie,Lone,Emil,Tobias,Linus,Robin
join()
The join() method is used to convert all elements in the array into a string.
Parameter: optional. Specifies the delimiter to use. If this parameter is omitted, a comma is used as the separator.
Return value: returns the connected string
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var energy = fruits.join(); result: Banana,Orange,Apple,Mango
slice()
The slice() method returns the selected element from an existing array.
The slice() method extracts a part of a string and returns the extracted part with a new string.
Parameter: optional. array.slice(start, end) . If this parameter is omitted, all are intercepted.
Is a positive number: starting subscript (0) - > ending subscript (excluding)
Negative number: start (1) - > end (excluding)
Return value: returns the intercepted new array
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1,3); result: Orange,Lemon var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var myBest = fruits.slice(-3,-1); // Intercept the two elements from the penultimate (inclusive) to the penultimate (exclusive) var myBest = fruits.slice(-3); // Intercept the last three elements result: Lemon,Apple
2. Method of changing the original array
unshift()
The unshift() method adds one or more elements to the beginning of the array and returns a new length.
Parameter: required. Adds one or more elements to the start of the array.
Return value: returns the length of the new array
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple"); result: Lemon,Pineapple,Banana,Orange,Apple,Mango
push()
The push() method adds one or more elements to the end of the array and returns a new length.
Parameter: required. The element to add to the array.
Return value: returns the length of the new array
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi") result: Banana,Orange,Apple,Mango,Kiwi
shift()
The shift() method removes the first element of the array from it and returns the value of the first element.
Return value: returns the deleted element
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift() result: Orange,Apple,Mango
pop()
The pop() method deletes the last element of the array and returns the deleted element.
Return value: returns the deleted element
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); result: Banana,Orange,Apple
splice()
The splice() method is used to add or remove elements from the array.
Parameter: required, optional, optional. array.splice(index,howmany,item1,.....,itemX) .
index: where to add / remove elements. Starting subscript
howmany: specifies how many elements should be deleted, which can be "0"
Item1,..., itemx: the new element to be added to the array
Return value: if the element is deleted, the deleted element is returned
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,0,"Lemon","Kiwi"); result: Banana,Orange,Lemon,Kiwi,Apple,Mango
reverse()
The reverse() method is used to reverse the order of the elements in the array.
Return value: array after reverse order
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.reverse(); result: Mango,Apple,Orange,Banana
sort()
The sort() method is used to sort the elements of the array.
Parameter: optional. Specify the sort order. Must be a function.
Return value: changed array
// Alphabetic ascending order var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); result: Apple,Banana,Mango,Orange // Numeric ascending order var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b}); Result: 100,40,25,10,5,1 // Number descending order var points = [40,100,1,5,25,10]; points.sort(function(a,b){return b-a}); Results: 1,5,10,25,40,100