https://blog.youkuaiyun.com/u013055396/article/details/60465964
arrayObject.slice(begin,
end
)
赋值数组中的元素,将其作为一个新数组返回,不会改变原数组。
切片含头不含尾,不包括end,end参数可以不传参。
还可以用于字符串String.slice();
let arr = [1,2,3,'hello world',4.12,true];
arr.slice(2,5);
区别:stringObject.split(separator,howmany)
把一个字符串分割成字符串数组,howmany可选参数,返回的子串不会多于这个参数指定的数值。
arrayObject.splice(start[, deleteCount[, item1[, item2[, ...]]]])
删除从 start 索引处开始的deleteCount(零个或多个)个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。
返回值含有被删除元素的数组(deleteCount=0时,返回空数组)。
var test = [];
test[1] = {name:'1',age:1};
test[2] = {name:'2',age:2};
test[4] = {name:'3',age:3};
var arr1 = test.splice(2,1);
console.log(test);
console.log(arr1);
输出结果:
原数组长度相应改变,但是原数组索引也相应改变,splice参数中第一个2,是删除起始索引(从0算起),在此是数组第二个元素。第二个1,删除元素的个数,在此只删除一个元素test[2]。
var test = [];
test[1] = {name:'1',age:1};
test[2] = {name:'2',age:2};
test[4] = {name:'3',age:3};
var arr1 = test.splice(2,0,{name:'6',age:6});
console.log(test);
console.log(arr1);
输出结果:
区别:delete方法删除数组元素
var test = [];
test[1] = {name:'1',age:1};
test[2] = {name:'2',age:2};
test[4] = {name:'3',age:3};
delete test[2];
console.log(test);
输出结果为:
该方式数组长度不变,此时test[2]为undefined,好处是原来数组索引也保持不变,此时要遍历数组元素可以用这种遍历方式跳过其中undefined元素即可,所以非常实用。
var test = [];
test[1] = {name:'1',age:1};
test[2] = {name:'2',age:2};
test[4] = {name:'3',age:3};
delete test[2];
console.log(test);
for(i in test){
console.log(test[i])
}
输出结果:
push()
pop()
shift()
unshift()
reverse()
sort()
indexOf()
lastIndexOf()
concat(arrayX,......,arrayX)
array.every(function(currentValue,index,arr)[, thisValue])
array.some(function(currentValue,index,arr)[,thisValue])
filter()
array.forEach(function(currentValue, index, arr)[, thisValue])
array.map(function(currentValue[,index,arr])[, thisValue])
arrayObject.join(separator)
清空数组
// 方式1 推荐
arr = [];
// 方式2
arr.length = 0;
// 方式3
arr.splice(0, arr.length);