创建一个数组
let arr = []
let arr = new Array()
let arr = new Array(2)
let arr = new Array(2).fill("")
数组的方法
let arr = [1, 2, 3, 4]
let target = arr.slice(0, 2)
console.log(target)
let arr1 = [1, 2, 3, 4]
let arr2 = [5, 6]
let target = [].concat(arr1, arr2)
let target2 = arr1.concat(arr2)
console.log(target)
console.log(target2)
let arr = [1, 2 ,3, 4]
let target = arr.join("-")
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.toString()
console.log(target)
console.log(arr)
let arr = [1, 2, 3, 4]
let target = arr.includes(1)
console.log(arr)
console.log(target)
let arr = [[1, 2], 3, 4, [5, 6, 7]]
let target = arr.flat()
console.log(arr)
console.log(target)
let arr = [1, 2, ,4, 5]
let target = arr.flat()
console.log(arr)
console.log(target)
let arr = [[1,2], [3,4], [[5, 6], [7, 8]], 9]
let target = arr.flatMap((x) => x)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.reverse()
console.log(arr)
console.log(target)
let arr = [1, 3, 2, 4]
let target = arr.sort()
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.shift()
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.unshift(0)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.pop()
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
let target = arr.push(5)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4, 5]
let target = arr.splice(0, 2)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4, 5]
let target = arr.splice(1, 2)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4, 5]
let target = arr.splice(0, 2, 8)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4, 5]
let target = arr.splice(2, 2)
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4, 5]
let target = arr.splice(2, 0, 6)
console.log(arr)
console.log(target)
数组遍历方法
let arr = [1, 2, 3, 4]
const trget = arr.forEach((item, index, list) => {
item = item * 2
})
console.log(arr)
console.log(trget)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.forEach((item, index, list) => {
item.num = item.num * 2
return item
})
console.log(arr)
console.log(trget)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.map((item, index, list) => {
item.num = item.num * 2
return item
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.map((item, index, list) => {
item.num = item.num * 2
if (index === 1) {
}
if (index !== 1) {
return item
}
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.filter((item, index, list) => {
return item.num === 2
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.filter((item, index, list) => {
return item.num > 1
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.every((item, index, list) => {
return item.num > 0
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.every((item, index, list) => {
return item.num > 2
})
console.log(arr)
console.log(target)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.some((item, index, list) => {
return item.num > 2
})
console.log(arr)
console.log(target)
let arr = [1, 2, 3, 4]
const sum = arr.reduce((prev, item) => {
return prev + item
}, 0)
console.log(sum)
console.log(arr)
let arr = [{num: 1}, {num: 2}, {num: 3}, {num: 4}]
const sum = arr.reduce((prev, item) => {
return prev + item.num
}, 0)
console.log(sum)
let arr = [{num: 1}, {num: 2}, {num: 3}]
const target = arr.find((item, index, list) => {
return item.num > 0
})
console.log(arr)
console.log(target)