1.js中数组的操作
(部分操作原数组,部分形成新的数组)
1. pop
var arr = [1,2,3,4,5];
arr.pop()//从数组最后删除
console.log(arr)// [1, 2, 3, 4]
2.push
var arr = [1,2,3,4,5];
arr.push(6,7)//在最后添加数据
console.log(arr)// [1, 2, 3, 4, 5, 6, 7]
3.shift
var arr = [1,2,3,4,5];
arr.shift()//从开头删除
console.log(arr)//[2, 3, 4, 5]
4.unshift
var arr = [1,2,3,4,5];
arr.unshift(99,100)//从开头加入数据
console.log(arr)
5.concat
var arr = [1,2,3,4,5];
var arr1 = arr.concat(6,7,8,9,10);//连接2个数组
console.log(arr1)// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6.splice
array.splice(index,howmany,item1,.....,itemX)
//index:规定从何处添加/删除元素。
//howmany:规定应该删除多少元素。必须是数字,但可以是 "0"。
//item1, ..., itemX 可选。要添加到数组的新元素
var arr = [1,2,3,4,5];
var arr1 = arr.splice(2,2,8,9,10);
console.log(arr)//[1, 2, 8, 9, 10, 5]
console.log(arr1)//[3, 4]
7.reverse
var arr = [1,2,3,4,5];
arr.reverse();//反转数组数值顺序
console.log(arr)//[5, 4, 3, 2, 1]
8.slice
var arr = [1,2,3,4,5];
var arr1 = arr.slice(1,4);//从规定位置取出数据
console.log(arr1)// [2, 3, 4]
9.数组去重
var arr = [13,24,34,24,15,46,46,27,34,18,13,39,10];
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
if( arr.indexOf(arr[i])==i)
{
arr1.push(arr[i]);
}
}
console.log(arr1);//[13, 24, 34, 15, 46, 27, 18, 39, 10]
2.js中使用typeof能得到哪些类型?
undefined、string、number、boolean、object、function
转载:https://blog.csdn.net/lemon94924/article/details/101349860
查看评论