数组操作

  • filter
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.filter(it => it.id === "1")   // [{"id":"1","name":"张三"}]
a.filter(it => it.aaa === "1")   或者  a.filter(it => it.id === "3")   // []
  • map
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.map(it=>it.id)  // ['1', '2']
a.map(it=>it.aaa) // [undefined, undefined]
a.map(it=>it.id==="1") // [true, false]

 

  • find
复制代码
语法:array.find(function(currentValue, index, arr),thisValue)
currentValue 必需。当前元素
index 可选。当前元素的索引值
arr 可选。当前元素所属的数组对象
 
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
箭头函数
a.find(it=>it.id==='1') // {id: '1', name: '张三'}
a.find(it=>it.id==='3') 或者 a.find(it=>it.aaa==='1') // undefined
复制代码
  • every
const a = [{"id":"1","name":"张三"}]
a.every(it=>it.id==='1') // true
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.every(it=>it.id==='1') // false
  • unshift 将指定的数值添加到数组的开头
const a = ['1','2','3']
a.unshift('-1') // 4
a // ['-1','1','2','3']
  • push 将指定的数值添加到数组的尾部
const a = ['1','2','3']
a.push('4') // 4
a // ['1','2','3','4']
  • .pop 删除数组最后一个元素
const a = ['1','2','3']
a.pop() // '3'
a // ['1', '2']
  • .shift 删除数组第一个元素
const a = ['1','2','3']
a.shift() // '1'
a // ['2','3']
  • .splice
复制代码
(1)纯删除   数组.arr.splice(起始下标,删除数量)
    从起始下标开始,删除几个
(2)添加    数组.arr.splice(起始下标,删除数量,...插入元素)
   从起始下标开始,删除几个,并在该位置添加插入的元素
返回值 : 返回的是刚才删除的元素.
// 删除
const a = ['1','2','3']
a.splice(1,1) // ['2']
a // ['1','3']
 
//  删除并插入
const a = ['1','2','3']
a.splice(1, 1, '2-1', '2-2') // ['2']
a // ['1', '2-1', '2-2', '3']
复制代码
  • contact 两个数组合并
复制代码
const a = ['1','2','3']
const b = ['2','3','4']
a.concat(b) // ['1', '2', '3', '2', '3', '4']
a // ['1', '2', '3']
b // ['2', '3', '4']
Array.from(new Set(a.concat(b))) // 去重 ['1', '2', '3', '4']
a.concat('7',b) // ['1', '2', '3', '7', '2', '3', '4']
a.concat('7'+b) // ['1', '2', '3', '72,3,4']
a + b // '1,2,32,3,4'
复制代码
  • join 数组转字符串
const a = ['1','2','3']
a.join('-') // '1-2-3'
  • reverse 数组顺序颠倒
const a = ['1','2','3']
a.reversed() // ['3', '2', '1']
  • sort 排序
const a = [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.sort(function(a,b) {
   return a.id-b.id
}) // 升序 [{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
a.sort(function(a,b) {
   return b.id-a.id
}) // 降序 [{"id":"2","name":"李四"},{"id":"1","name":"张三"}]
  • reduce 为每一个元素执行一次回调,并得到结果
复制代码
语法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
total 必需。初始值, 或者计算结束后的返回值。
currentValue 必需。当前元素
currentIndex 可选。当前元素的索引
arr 可选。当前元素所属的数组对象。
initialValue 可选。传递给函数的初始值

const a = [{"id":"1","value":1},{"id":"2","value":2}]
a.reduce((sum,num)=>{
    return sum + num.value
},10) // sum初始值是10
10+1+2 // 13
 
const a = [{"id":"1","value":1},{"id":"2","value":2}]
a.reduce((obj,num)=>{
    obj.push(num.id);
    return obj
}, []) // ['1', '2']
 
// 当元素是reduce函数的输出时,不能给元素赋值
复制代码
  • slice 数组复制
语法:slice(start,end)
start 参数必须(不包含),规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置
end 参数不必须(包含),规定从何处结束选取
let a = [1,2,3,4,5,8,9,6,2,4,5]
a.slice(3) // [4, 5, 8, 9, 6, 2, 4, 5]
a.slice(3, 6) // [4, 5, 8]
a.slice(-3) // [2, 4, 5]
a.slice(-3, -1) // [2, 4]
  • includes
复制代码
语法:slice(searchElement, ?fromIndex)
searchElement 参数必须,查询的内容
fromIndex 参数可选,起始下标,可为负数,负数代表从尾部开始查
const a = ['1','2','3','4','5']
a.includes('1') // true
a.includes('1', 0) // true
a.includes('1', 1) // false
a.includes('4', -1) // false
a.includes('4', -2) // ture

 

// 与findindex相比,如果数组中存在NaN,findindex会误判,includes不会
const a = ['1','2','3','4','5',NaN]
a.findIndex(it => it === NaN) // -1
a.includes(NaN) // -1
a.findIndex(it => isNaN(it)) // 5 findindex解决方案
复制代码
 
 
posted @   卤煮  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示