comparison of JavaScript array methods
As we all know, JavaScript provides many ways to change arrays, but some of them can affect the original array, and some do not. The list is listed below.
first, the newaffect the original array
Array.push ()//adds one or more elements to the end of the array and returns the new length.
var array =[1,2,3,4,5];array.push (// [1,2,3,4,5,6];
Array.unshift ()//adds one or more elements to the beginning of the array and returns the new length.
var array =[1,2,3,4,5];array.unshift (// [6,1,2,3,4,5];
does not affect the original array
Array.concat ()//joins two or more arrays and returns the result.
var arr1 = [1,2,3,4// [1,2,3,4,a,b]
ii. Removalaffect the original array
Arry.pop ()//delete and return the last element of the array
var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.pop (); // Banana,orange,apple
Arry.shfit ()//delete and return the first element of the array
var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.shift () // Orange,apple , Mango
Arry.splice ()//deletes the element and adds a new element to the array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice (2,0, "Lemon", "Kiwi"); // Banana,orange,lemon,kiwi,apple,mango
does not affect the original array
Array.filter ()//creates a new array in which the elements in the new array are checked by examining all the elements in the specified array that meet the criteria.
var ages = [+, +, +, +]; function Checkadult (age) { return -age >=;} function myFunction () { document.getElementById("Demo"). InnerHTML = ages.filter (checkadult);}
Third, replaceaffect the original array
Arry.splice ()//deletes the element and adds a new element to the array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice (//Banana, Orange,lemon,kiwi,apple,mango
does not affect the original array
The Array.map ()//method returns a new array in which the elements of the array call the function-processed values for the original array element.
var numbers = [4, 9, +--]; function myFunction () { = document.getElementById ("demo") = numbers.map (math.sqrt);}
JavaScript Array method Comparison (deep learning array)