1、清空数组
array_1.splice(0,array_1.length),两个参数,从前到后分别为:从数组索引为0的位置的元素开始,删除 array_1.length 个元素,结果 array_1 是方法执行后的值 为 [] ,即array_1数组元素被清空了,array_2 则是执行函数后被删除的元素,因为array_1被全删除,故 array_2 接收的结果就是 array_1 被删除前的值
let array_1 = ["1","2","3","4"]; let array_2 = array_1.splice(0,array_1.length); //array_1 结果 [] //array_2 结果 ["0", "1", "2", "3", "4"]
2、在指定位置插入数组
array_1.splice(0,0,"0"),三个参数,从前到后分别为:从数组索引为0的位置的元素开始,删除0个元素,插入一个 "0" 元素,结果 array_1 是在起始位置新增了一个 “0” 元素,array_2 则是接收的 splice 函数执行后中删除的数组元素,本次未删除任何元素,故 array_2 返回 为 []
let array_1 = ["1","2","3","4"];
let array_2 = array_1.splice(0,0,"0");
//array_1 结果 ["0", "1", "2", "3", "4"]
//array_2 结果 []
3、 在数组末尾增/删元素
push()
/ pop()
,var_1接收的是函数执行后数组元素的个数,var_2 接收的是函数执行后被删除的元素
let array_1 = ["1","2","3","4"];
var var_1 = array_1.push("5");
//array_1 结果 ["1", "2", "3", "4", "5"]
//var_1 结果 5
var var_2 = array_1.pop();
//array_1 结果 ["1", "2", "3", "4"]
//var_1 结果 "4"
4、 在数组首部增/删元素
unshift()
/ shift()
- 在数组首部增/删元素,var_1接收的是函数执行后数组元素的个数,var_2 接收的是函数执行后被删除的元素
let array_1 = ["1","2","3","4"];
var var_1 = array_1.unshift("0");
//array_1 结果 ["0", "1", "2", "3", "4"]
//var_1 结果 5
var var_2 = array_1.shift();
//array_1 结果 ["1", "2", "3", "4"]
//var_1 结果 "0"
5、把一个(或多个)数组和(或)值与原数组拼接,返回拼接后的数组
concat()
let array_1 = ["1","2","3","4"];
let array_2 = ["7", "8"];
var var_1 = array_1.concat(array_2);
//array_1 结果 ["1", "2", "3", "4", "5", "6"]
// var_1 结果 ["1", "2", "3", "4", "5", "6"]
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array