Home > Web Front-end > JS Tutorial > The three functions of slice, splice and split are difficult to distinguish in javascript_javascript skills

The three functions of slice, splice and split are difficult to distinguish in javascript_javascript skills

WBOY
Release: 2016-05-16 15:07:51
Original
1280 people have browsed it

Every time I see these three functions, I am confused and must search online; today, I happened to see them again, so it must be time to take notes about them

1.slice (array)

Usage: array.slice(start,end)

Explanation: This method intercepts part of the array and returns a copy of the array; the parameter start is the starting array index of the interception, and the end parameter is equal to the position value of the last character you want to retrieve plus 1 (optional)

//如果不传入参数二,那么将从参数一的索引位置开始截取,一直到数组尾
var a=[1,2,3,4,5,6];
var b=a.slice(0,3);  //[1,2,3]
var c=a.slice(3);    //[4,5,6]

//如果两个参数中的任何一个是负数,array.length会和它们相加,试图让它们成为非负数,举例说明:
//当只传入一个参数,且是负数时,length会与参数相加,然后再截取
var a=[1,2,3,4,5,6];
var b=a.slice(-1);  //[6]

//当只传入一个参数,是负数时,并且参数的绝对值大于数组length时,会截取整个数组
var a=[1,2,3,4,5,6];
var b=a.slice(-6);  //[1,2,3,4,5,6]
var c=a.slice(-8);  //[1,2,3,4,5,6]

//当传入两个参数一正一负时,length也会先于负数相加后,再截取
var a=[1,2,3,4,5,6];
var b=a.slice(2,-3);  //[3]

//当传入一个参数,大于length时,将返回一个空数组
var a=[1,2,3,4,5,6];
var b=a.slice(6);  //[]

Copy after login

2.slice(string)

Usage: string.slice(start,end)

Explanation: The slice method copies part of the string to construct a new string. The usage is the same as the slice method of parameter array array; the end parameter is equal to the position value of the last character you want to take plus 1

//举个简单的例子
var a="i am a boy";
var b=a.slice(0,6);  //"i am a"
Copy after login

3.splice (array)

Usage: array.splice(start,deleteCount,item...)

Explanation: The splice method removes one or more arrays from array and replaces them with new items. The parameter start is the starting position to remove elements from the array. The parameter deleteCount is the number of elements to be removed.

If there are additional parameters, the item will be inserted at the position of the removed element. It returns an array containing the removed elements.

//举一个简单的例子
var a=['a','b','c'];
var b=a.splice(1,1,'e','f');  //a=['a','e','f','c'],b=['b']
Copy after login

4.split (string)

Usage: string.split(separator,limit)

Explanation: The split method splits this string into fragments to create a string array. The optional parameter limit can limit the number of fragments that are split. The separator parameter can be a string or a regular expression. if

separator is an empty character and will return an array of single characters.

//再举一个简单的例子
var a="0123456";
var b=a.split("",3);  //b=["0","1","2"]
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template