// 创建空数组
let a1 = new Array();
let a2 = [];
// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];
// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];
//数组访问
console.log(a3[0]);
console.log(a4[0]);
// 创建并初始化数组
let a3 = new Array(1,2,3,4,5);
let a4 = [1,2,3,4,5];
//数组遍历
for(let i = 0; i < a3.length; i++){
console.log(a3[i]);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
//数组遍历
for(let n of a4){
console.log(n);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
//数组遍历
a4.forEach(function(item, index, array){
console.log(item)
})
// 创建并初始化数组
let a4 = [1,2,3,4,5];
//在尾部删除元素
console.log(a4.pop());
//在尾部插入元素
a4.push(6,7);
//数组遍历
for(let n of a4){
console.log(n);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
//在头部删除元素
console.log(a4.shift());
//在尾部插入元素
a4.unshift(6,7);
//数组遍历
for(let n of a4){
console.log(n);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let removed = a4.splice(1,2,6,7,8);
//数组遍历
for(let n of a4){
console.log(n);
}
for(let m of removed){
console.log(m)
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let sliced = a4.slice(1,3);
//数组遍历
for(let n of a4){
console.log(n);
}
for(let m of sliced){
console.log(m)
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let a5 = a4.concat([6,7],[8,9]);
//数组遍历
for(let n of a4){
console.log(n);
}
for(let m of a5){
console.log(m)
}
// 创建并初始化数组
let a4 = [1,15,2];
a4.sort();
//数组遍历
a4.forEach(function(item, index, array){
console.log(item)
})
a4.sort(function(a,b){return a-b;});
a4.forEach(function(item, index, array){
console.log(item)
})
// 创建并初始化数组
let a4 = [1,15,2,15];
console.log(a4.indexOf(15));
console.log(a4.lastIndexOf(15));
console.log(a4.indexOf(50));
let r1 = a4.find(function(item, index, array){
return item == 15;
});
console.log(r1);
let r2 = a4.findIndex(function(item, index, array){
return item == 15;
});
console.log(r2);
let r3 = a4.filter(function(item, index, array){
return item == 15;
});
console.log(r3);
console.log(a4.includes(15));
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.every(x=>x%2==0);
console.log(r1);
let r2 = a4.some(x=>x%2==0);
console.log(r2);
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.map(x=>x*2);
for(let n of r1){
console.log(n);
}
let r2 = a4.reduce((sum, current)=>sum + current,0);
console.log(r2);
let str = '1,2,3,4,5';
let a1 = str.split('\\,');
for(let n of a1){
console.log(n);
}
// 创建并初始化数组
let a4 = [1,2,3,4,5];
let r1 = a4.join('-');
console.log(r1);
let a2 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
alert(a2[1][1]);