JavaScript Array method Comparison (deep learning array)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.