Home > Web Front-end > JS Tutorial > body text

Array element deletion, replacement and addition

巴扎黑
Release: 2016-12-06 09:40:00
Original
1438 people have browsed it

Syntax: splice(index,len,[item]) This method will change the original array.
splice has 3 parameters, it can also be used to replace, delete or add one or several values ​​in the array
index represents the starting subscript of the array len represents the length of replacement/deletion item represents the replaced value, if the deletion operation occurs, the item is empty
Example:
1 Delete: var arr = ['a','b','c','d'];
arr.splice(1,2);
console.log(arr);----> ;The output is ['a','d']
2 Replacement: var arr2 = ['a','b','c','d'];
arr2.splice(1,2,'ttt') ;
console.log(arr2);---->The output is ['a','ttt','d']
3. Add (make len is 0, item is the added value):
var arr = ['a','b','c','d'];
arr.splice(1,0,'ttt');
console.log(arr);-----The output is [' a','ttt','b','c','d']

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