Blogger Information
Blog 94
fans 0
comment 0
visits 92645
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【JS】JS 数组操作:(压缩,打包,遍历,增删,断言,过滤,查找,归并-删除更新)
可乐随笔
Original
547 people have browsed it

JS 数组 API -1

包括对象在内,尽量用字面量来声明

  1. let arr = [1, 2, 'a', 'b', true, { x: 1, y: 2 }, [1, 2, 3], function () { }]
  2. console.log(arr)

1. …rest 压缩与展开

  1. //fn.apply(null,[1,2,3])
  2. arr = [1,2,3]
  3. let arr1 = [...arr]//数组完全复制
  4. console.log(arr1)
  5. arr = [...arr,4,5,6]
  6. console.log(arr)
  7. console.log('-------------------')

2. Array.of 打包

  1. let items = [1,2,3,4,5,6]
  2. console.log(Array.of(items))

3. Array.from() :

类数据(对象) -> 包装成一个真正的数组

  1. const linkArr = {
  2. 0:'red',
  3. 1:'blue',
  4. 2:'green',
  5. length:3
  6. }
  7. // 为什么要转真数组?
  8. // 因为数组上有非常多的方法可以使用
  9. const data = Array.from(linkArr)
  10. console.log(Array.isArray(data)?'Array':'No Array')

数组遍历方法

  1. for(let item of data){
  2. console.log(item)
  3. }
  4. console.log('=========================')

数组 API -2

增删受限: push:放入,pop:取出,unshift,shift

1. 尾部操作

  1. let arr2 = []
  2. console.log(arr2.push(10))
  3. console.log(arr2.push(20,30))
  4. //长度:3,分别是:10,20,30
  5. console.log(arr2.pop())
  6. console.log(arr2.pop())
  7. console.log(arr2.pop())
  8. console.log(arr2.pop())
  9. console.log(arr2.length)
  10. console.log(arr2)
  11. console.log('------------------')

2.头部操作

  1. //添加 unshift,注意顺序
  2. //头部取出 shift
  3. console.log(arr2.unshift(10))
  4. console.log(arr2.unshift(30,20))
  5. console.log(arr2)
  6. console.log(arr2.shift())
  7. console.log(arr2)
  8. console.log('------------------')
  9. // * 3.删除
  10. arr3 = [1,2,3,4]
  11. //删除第2个索引上的值,但索引还在
  12. delete arr3[2]
  13. console.log(arr3)
  14. //输出:[ 1, 2, <1 empty item>, 4 ]
  15. console.log('=========================')

数组 API -3

迭代方法,遍历元素

  • 1.forEach,map
  • 2.every,some
  • 3.filter,find,findIndex
  • 4.reduce

let arr4 = [1,2,3,4]

1. forEach:没有返回值,参数:值、索引、数组

  1. arr4.forEach(function(item,key,arr4){
  2. console.log(item,key,arr4)
  3. })
  4. //只有item(值)是必须的,其他两个参数可不写
  5. console.log('------------------')

2. map:有返回值

  1. let result = arr4.map(function (item,key,arr4){
  2. return item * 2
  3. })
  4. console.log(result)
  5. console.log('------------------')

3. every,some:断言,true /false

  1. //every():全部满足条件才返回true,否则为false
  2. console.log(arr4.every(item => item >= 0)) //true
  3. console.log(arr4.every(item => item >= 3)) //false
  4. //some():只要有一个值满足:返回true
  5. console.log(arr4.some(item => item >= 1)) //true
  6. console.log(arr4.some(item => item >= 10)) //false
  7. console.log('------------------')

4. filter:过滤器,将满足条件的元素打包到一个数组中返回

  1. console.log(arr4.filter(item => item >= 2))
  2. //返回[2.3.4]
  3. console.log('------------------')

5. find:查找,从结果中返回第一个,满足条件的元素

  1. console.log(arr4.find(item => item >= 3))
  2. //返回:3
  3. console.log(arr4.findIndex(item => item >= 3))
  4. //返回索引值:2
  5. console.log('------------------')

6. reduce: 归并

  1. /**
  2. * * 1. 回调函数
  3. * * 2. 初始值,如0,''
  4. */
  5. /**
  6. * 回调函数说明:
  7. * * 1. acc: 累加器,结束不断的累加给它,最终返回也是它
  8. * * 2. cur: 当前元素
  9. * * 3. key: 当前元素的索引
  10. * * 4. arr: 当前正在遍历的数组
  11. * ? cur, key, arr, 与之前的迭代方法参数一样
  12. * ? init 为初始值
  13. */
  14. // arr.reduce(function(acc,cur,key,arr){},init)
  15. result = arr4.reduce(function(acc,cur,key,arr){
  16. //观察执行过程
  17. console.log(`acc = ${acc}, cur = ${cur}, key = ${key}, arr= [${arr}]`)
  18. return acc+cur
  19. },0)
  20. //将数组内容进行累加,初始值为10
  21. console.log(result)

数组常用 API-4

1. sort:排序

  1. let arr = [10, 1, 22, 8]
  2. console.log(arr.sort(function (a, b) {
  3. return a - b
  4. }))
  5. //简化成箭头函数,省略 function 和 {}
  6. //升序 asc
  7. console.log(arr.sort((a, b) => a - b))
  8. //降序 desc
  9. console.log(arr.sort((a, b) => b - a))
  10. arr = arr.sort((a, b) => a - b)//对数组排序
  11. console.log(arr)
  12. console.log('----------------------')

2. join:array->string字符串数组拼接

  1. arr = ['<ul>\n', ' <li>xxx</li>\n', '</ul>']
  2. console.log(arr.join(''))
  3. //模板字面量更方便
  4. console.log('----------------------')

3. slice():子元素

  1. //slice(2,3),参数2,3:从索引2取到索引3,一个数值
  2. arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. console.log(arr.slice(2,6))
  4. console.log(arr.slice(-4,-2))//取反
  5. console.log('----------------------')

4. splice():删除(扩展功能:新增,更新)

  1. //4.1 删除
  2. console.log(arr)
  3. //从第2个元素开始,删除2个元素,返回删除的数组
  4. console.log(arr.splice(1,2))
  5. console.log(arr)
  6. //4.2 更新,
  7. console.log(arr.splice(1,2,'a','b'))
  8. console.log(arr.splice(1,2,['a','b']))
  9. console.log(arr.splice(1,2,...['a','b']))
  10. console.log(arr)
  11. //4.2 新增
  12. //删除的数据,必须小于新增的元素数据
  13. //第2个参数为0,第3个参数只要不是一个,就实现新增/插入/insert
  14. console.log(arr.splice(4,0,'x','y'))
  15. console.log(arr)
  16. console.log('----------------------')
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post