文章目录

    • -1 序言
    • 0. delete 数组元素的删除
    • 1. push 数组元素的添加
    • 2. pop 删除数组末尾元素
    • 3. unshift 数组最前面添加元素
    • 4. shift 删除数组最前面的元素
    • 5 . splice 数组元素的截取
    • 6. reverse 数组元素的反转
    • 7. sort 数组元素的排序
    • 8. concat 数组的拼接
    • 9. join 数组元素的拼接
    • 10. split 字符串的切割
    • 11. indexOf 数组元素的查找
    • 13. lastIndexOf 数组元素的查找
    • 13. includes 数组元素的查找
    • 14. slice 数组的截取
    • 16. isArray 判断是否为数组
    • 16. toString
    • 17. flat 数组的扁平化
    • 18. Array 创建一个数组实例
    • 19. fill 数组的填充
    • 20. from 转换类数组为标准的数组
    • 21 fouEach 数组的遍历
    • 22. map 数组的遍历并映射
    • 23. filter 数组的遍历并过滤
    • 24. reduce

-1 序言

0. delete 数组元素的删除

 var arr = [1,6,8,9,6]
 delete arr[3]
 console.log(arr)   //  [1, 6, 8, empty, 6]
 console.log(arr[3])  //  undefined

1. push 数组元素的添加

2. pop 删除数组末尾元素

3. unshift 数组最前面添加元素

4. shift 删除数组最前面的元素

5 . splice 数组元素的截取

var arr = [1,5,6,6,9,7,8,25,4]
var newArr = arr.splice(2) arr = [1,5]   newArr = [6,6,9,7,8,25,4]
var newArr = arr.splice(2,3) arr = [1,5,7,8,25,4]   newArr = [6,6,9]
var newArr = arr.splice(2,3,'aa',5,'6','bb') arr = [1,5,'aa',5,'6','bb',7,8,25,4]   newArr = [6,6,9]

6. reverse 数组元素的反转

var arr = [1,5,6,9]
var newArr =  arr.reverse()
 console.log(arr === newArr)  //true
 console.log(arr)     [9,6,5,1]
 console.log(newArr)   [9,6,5,1]

7. sort 数组元素的排序

  var arr = [1,2,3,5,'6',8,7,'22',23,45]
  var newArr = arr.sort()
 console.log(arr === newArr)  //true
 console.log(arr)       [1, 2, "22", 23, 3, 45, 5, "6", 7, 8,]
 console.log(newArr)   [1, 2, "22", 23, 3, 45, 5, "6", 7, 8,]
 arr.sort(function(a,b){return a-b})  // 升序
 console.log(arr)    [1, 2, 3, 5, "6", 7, 8, "22", 23, 45]
 console.log(newArr)    [1, 2, 3, 5, "6", 7, 8, "22", 23, 45]
  arr.sort(function(a,b){return b-a})  // 降序
   console.log(arr)          [45, 23, "22", 8, 7, "6", 5, 3, 2, 1]
   console.log(newArr)        [45, 23, "22", 8, 7, "6", 5, 3, 2, 1]

8. concat 数组的拼接

 var arr = [1,2,3]
 var arr1 = [25,36,78]
 var arr2 = [25,78,1,45]
 var newArr = arr.concat(arr1,arr2)
console.log(arr)        [1, 2, 3]
console.log(newArr)     [1, 2, 3, 25, 36, 78, 25, 78, 1, 45]
console.log('newArr===arr1',newArr===arr1) // false

9. join 数组元素的拼接

    var arr = [1,2,3]
  var newArr = arr.join()
  console.log(newArr)  // 1,2,3
    var newArr = arr.join('')
  console.log(newArr)  // 123
     var newArr = arr.join('aa')
  console.log(newArr)  // 1aa2aa3

10. split 字符串的切割


 var str = '465a76a986a6769'
 var arr = str.split()
 console.log(arr)           //  ["465a76a986a6769"]
 var arr = str.split('')  
 console.log(arr)           //  ["4", "6", "5", "a", "7", "6", "a", "9", "8", "6", "a", "6", "7", "6", "9"]
 var arr = str.split('6')
 console.log(arr)          //   ["4", "5a7", "a98", "a", "7", "9"]
 var arr = str.split('a')
 console.log(arr)          //   ["465", "76", "986", "6769"]

11. indexOf 数组元素的查找


 var arr = [1,6,8,9,6]
var newArr = arr.indexOf(3)   //  -1
 console.log(newArr)
 var newArr = arr.indexOf(6)
 console.log(newArr)       //  1

13. lastIndexOf 数组元素的查找


 var arr = [1,6,8,9,6]
var newArr = arr.indexOf(3)   //  -1
 console.log(newArr)
 var newArr = arr.indexOf(6)
 console.log(newArr)       //  4

13. includes 数组元素的查找

var arr = [1,2,3,5]
var a  = arr.includes(6)   // false
var a  = arr.includes(2)   // true

14. slice 数组的截取

const arr = [1,2,3]
const newArr = arr.slice(1,2)
console.log('aa1',newArr) // aa1 [ 2 ]
console.log('aa2',arr) // aa2 [ 1, 2, 3 ]

16. isArray 判断是否为数组

var arr = [1,2,3]
console.log(Array.isArray(arr))  // true
var arr = {name:123}
console.log(Array.isArray(arr))  // false

16. toString

let arr = [1, 2, 3, 4, 5];
  let str = arr.toString()
  console.log(str)// 1,2,3,4,5

17. flat 数组的扁平化

const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat()); // 默认depth 为1
const arr2 = [0, 1,undefined,null,'', 2, [[[3, 4]]]];// 全局属性 Infinity 是一个数值,表示无穷大。
console.log('Infinity',Infinity) // Infinity
console.log('Infinity typeof',typeof Infinity) // number
// Infinity 展开任意深度
console.log(arr2.flat(Infinity)); // [ 0, undefined, null, '', 2, 3, 4 ]
const arr = new Array(2) // // flat会移除数组中的空项
arr[0] = 'aaaa'
console.log(arr.flat()) // [ 'aaaa' ]

18. Array 创建一个数组实例

19. fill 数组的填充

[1, 2, 3].fill(4);               // [4, 4, 4]
[1, 2, 3].fill(4, 1);            // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2);         // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1);         // [1, 2, 3]
[1, 2, 3].fill(4, 3, 3);         // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2);       // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN);     // [1, 2, 3]
[1, 2, 3].fill(4, 3, 5);         // [1, 2, 3]
Array(3).fill(4);                // [4, 4, 4]

20. from 转换类数组为标准的数组

const set = new Set(['foo', 'bar', 'baz', 'foo']);
console.log(Array.from(set)) // [ 'foo', 'bar', 'baz' ]
function f() {
    return Array.from(arguments);
}
  console.log(  f(1, 2, 3)) // [ 1, 2, 3 ]

21 fouEach 数组的遍历

const arr = [1,2,3]
arr.forEach((ele,index,arr) =>{})

22. map 数组的遍历并映射

const arr = [1,2,3]
const newArr = arr.map((ele,index,arr) =>{
     return 1
})
console.log('11',newArr) // 11 [ 1, 1, 1 ]

23. filter 数组的遍历并过滤

const arr = [1,2,3]
const newArr = arr.filter((ele,index,arr) =>{
     return ele > 1
})
console.log('11',newArr) // 11 [ 2, 3 ]

24. reduce

const arr = [1,2,3]
const total = arr.reduce((pre,cur,index,arr) =>{
   return pre + cur
},0)
console.log('aa',total) // 6

发表回复