JS array of concat, slice, splice method

Source: Internet
Author: User

Methods of arrays

//1. Concat () (concat in English: merging arrays; merging multiple strings;)

var colors = ["Red", "green", "Blue"];

var Color2 = Colors.concat ("Yellow", ["Black", "Brown"]);

console. log (colors);//["Red", "green", "Blue"

console. log (Color2);//["Red", "green", "blue", "yellow", "black", "Brown"]

//Summary: concat merge array, original array unchanged, new array is passed in the original array and concat array of arguments

//2. Slice () Slice 's English interpretation: cut into pieces;

var colors = ["Red", "green","Blue", "yellow", "Purole");

var Color2 = Colors.slice (1);

var Color3 = Colors.slice (1, 3);

console. log (colors);//"Red", "green", "blue", "yellow", "purole"]

console. log (Color2);//["Green", "blue", "yellow", "purole"]

console. log (Color3);//["Green", "blue"]

//Summary: Slice (x, y) cut an array, the original array is unchanged, X begins to cut the array subscript, the position where Y ends but excludes the position is the value of the previous position of Y subscript, the value of x and Y can be negative, the last element of the array is subscript 1, if x, Y is negative, Then Array.slice (x, y) is the same as the result of Array.Length (x + array.length, y + aray.length). If there is only one value, start the cut from that value until the end of the array

//3. Splice () (the most powerful array method) (Splice English interpretation : stranding, twisting (two-segment rope), adhesive bonding, bonding (film, tape, etc.);

//delete

var colors = ["Red", "green", "Blue"];

var Remove = Colors.splice (0, 1);

console. log (colors);//["Green", "blue"]

console.log (remove); ["Red"]

//Insert

Remove = Colors.splice (1, 0, "Yellow", "Orange");

console. log (colors);//["green", "yellow", "orange", "Blue"]

console.log (remove); //[]

//Replace

Remove = Colors.splice (1, 1, "Red", "purple");

console. log (colors);//["Green", "red", "purple", "orange", "Blue"

console.log (remove); ["Yellow"]

//Summary: Splice (x, y) two parameters of the time for deletion, X is the position to start deletion, Y is the number of deletions , The return value is the deleted array element

//Splice (x, 0, "Red", "green"), X is the position to begin deletion, 0 is the number of deletions, the third parameter is inserted after the X position of the array, and the return value is a Empty array, because there's nothing.

// splice (x, Y, "red", "green"), X is the position to begin deletion, Y is the number of deletions, first deleted and then added from the X position is replaced , The return value is /c1> the deleted array element

JS array of concat, slice, splice method

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.