Home  >  Article  >  Web Front-end  >  Array element deletion, replacement and addition

Array element deletion, replacement and addition

巴扎黑
巴扎黑Original
2016-12-10 09:11:491274browse

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']

Statement:
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