数组的声明两种方式
var arr01 = [12,"hello",true];
console.log(arr01,typeof arr01); // "object"
var arr02 = new Array(12,"hello",true);
console.log(arr01,typeof arr02);
console.log(arr01.length);
数组的常用方法
// 1 插入和删除
arr.push("a");
arr.push([111,222]);
console.log(arr);
var ret = arr.pop();
console.log(ret);
console.log(arr);
arr.unshift(100);
console.log(arr);
arr.shift();
console.log(arr);
// 2 反转和排序
var arr02 = [1,4,3,6,2];
var arr02 = [1,4,10,6,2,100];
arr02.reverse();
console.log(arr02);
function sort_num(a,b){
return a-b
}
arr02.sort(sort_num);
console.log(arr02);
// splice方法
var arr = [1,2,3,4,5];
// (1)删除多个元素
arr.splice(2,2);
console.log(arr);
// (2) 添加多个元素
// arr.splice(2,0,100,101,102);
// console.log(arr);
// (3) 替换多个元素
// arr.splice(2,2,33,44);
// console.log(arr);
var arr1 = [1,2,3];
var arr2 = [4,5,7];
var ret = arr1.concat(arr2);
console.log( ret );
// join方法
var s = "zhangsan li wangwu";
var ret = s.split(" ");
console.log(ret);
var s2 = ret.join("-");
console.log(s2);
console.log(arr1.includes(2));
console.log(arr1.indexOf(3));
console.log(Array.isArray(arr1));
// 切片操作
arr3 = [111,222,333,444,555,666];
console.log(arr3.slice(1,3));
console.log(arr3.slice(1,-1));
console.log(arr3.slice(1));
console.log(arr3.slice());
console.log(arr3.slice(-3,-2));
console.log(arr3.slice(-2,-3)); // []
// 数组循环
for (var i=0;i<arr3.length;i++){
// 循环体
console.log(i,arr3[i])
}