const arr = [1, 2, 3];
for (let item of arr) {
console.log(item);
}
// 输出:1 2 3
const arr = [1, 2, 3];
for (let key in arr) {
console.log(key);
}
// 输出:0 1 2
const arr = [1, 2, 3];
arr.forEach((item, index) => {
console.log(item);
console.log(index);
});
// 输出:1 0 2 1 3 2
let arr = [1, 2, 3];
arr = arr.map(item => item + 1);
console.log(arr);
// 输出:[2, 3, 4]
注:sort中的方法第一个参数是从第二项开始的
返回值小于0:current在previous之前
返回值大于0:current在previous之后
let arr = [1, 2, 3];
arr = arr.sort((current, previous) => previous - current);
console.log(arr);
// 输出:[3, 2, 1]
arr = arr.sort((current, previous) => current - previous);
console.log(arr);
// 输出:[1, 2, 3]
let arr = [1, 2, 3];
arr = arr.reverse();
console.log(arr);
// 输出:[3, 2, 1]
let arr = [1, 2, 3];
arr = arr.filter(item => item >= 2);
console.log(arr);
// 输出:[2, 3]
const arr = [1, 2, 3];
const a = arr.find(item => item === 2);
console.log(a);
// 输出:2
const arr = [1, 2, 3];
const b = arr.some(item => item === 2);
console.log(b);
// 输出:true
const b2 = arr.some(item => item === 0);
console.log(b2);
// 输出:false
const arr = [1, 2, 3];
const b = arr.every(item => item === 2);
console.log(b);
// 输出:false
const b2 = arr.every(item => item > 0);
console.log(b2);
// 输出:true
const arr = [1, 2, 3];
const s = arr.join(",");
console.log(s);
// 输出:1,2,3
const arr = [1, 2, 3];
const arr2 = [4, 5, 6];
const a = arr.concat(arr2);
console.log(a);
// 输出:[1, 2, 3, 4, 5, 6]
arr.slice(起始位置(从0开始),结束位置(不包含));
const arr = [1, 2, 3];
const a = arr.slice(1, 2);
console.log(a);
// 输出:[2]
arr.splice(起始位置(从0开始), 替换数量, 替换数据(没有的话相当于删除))
const arr = [1, 2, 3];
const a = arr.splice(0, 1);
console.log(arr);
console.log(a);
// 输出:[2, 3] [1]
const arr = [1, 2, 3];
arr.push(4);
console.log(arr);
// 输出:[1, 2, 3, 4]
const arr = [1, 2, 3];
const a = arr.pop();
console.log(arr);
console.log(a);
// 输出:[1, 2] 3
const arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
// 输出:[0, 1, 2, 3]