Notes on array object splice method

Precautions for the splice method in JS

Notes on splices

1. splice() belongs to the method of array object, if the object uses splice, an error will be reported:

{
    
    a:1}.splice
运行结果:Uncaught SyntaxError: Unexpected token .

2. splice will change the original array (slice will not):

	var arr = [0,1,2,3];
	arr.splice(2,1);
	console.log(arr)
运行结果:[0, 1, 3]

3. When only inserting elements, insert from the left of the index:

var arr = [0,1,2,3];
arr.splice(2,0,"b");
console.log(arr);
运行结果: [0, 1, "b", 2, 3]

Guess you like

Origin blog.csdn.net/wzySan/article/details/103632083