JS数组方法集合 🐤
数组常用方法 | 输入 | 返回 | 解释 |
---|---|---|---|
arr.join()/arr.join(‘-’) | 空、- | ‘123’/‘1-2-3’ | 拼接 |
arr.push(‘123’,‘456’) | 元素 | 数组length | 尾加 |
arr.pop() | no | 删除的对象 | 尾删 |
arr.shift() | no | 删除的对象 | 头删 |
arr.unshift(‘123’,‘456’) | 元素 | 数组length | 头加 |
arr.sort((a, b) => a - b) | 函数 | 改变原数组 | 排序 |
arr.reverse() | 空 | 改变原数组 | 颠倒 |
arr.concat() | 数组 | 新数组 | 合并 |
arr.slice(index1,index2) | 数字 | 新数组 | 切片 |
arr.splice(index, howmany, item1, …, itemX)第二参不为 0 | 数字 | 改变原数组 | 删除 |
arr.splice(0,2,‘aaa’)第二参不为 0 | 数字+元素 | 改变原数组 | 替换 |
arr.splice(2,0,‘aaa’)第二参为 0 | 数字+元素 | 改变原数组 | 添加 |
arr.indexOf (item, start) | 数字 | 位置(前) | 查找 |
arr.lastindexOf (item, start) | 数字 | 位置(后) | 查找 |
arr.forEach ((item,index,arr) =>{}) | 函数 | 空 | 遍历,无法在中间被停止(抛出错误可强制) |
arr.map ((item) =>{return currentVal+1}) | 函数 | 新数组 | 计算属性 |
arr.filter ((item,index,arr) =>{}) | 函数 | 新数组 | 过滤 |
arr.some ((item, index , arr) => {}) | 函数 | true or false | 存在,return true 退出循环 |
arr.every ((item, index , arr) => {}) | 函数 | true or false | 全真 |
arr.findIndex ((item, index , arr) => {}) | 函数 | index | 查下标,剩下的元素则不再执行 |
arr.reduce ((sum, item) => {}, 0) | 函数 | index | 查下标,剩下的元素则不再执行 |
arr.includes(searchElement , fromIndex) | 元素+数字 | true or false | 查找 |
Array.form(item) | 数组对象 | 新数组 | 转换 |
Array.of(1,‘a’,true,{b:1}) | 任何 | 新数组 | 拼装 |
arr.fill(value, start, end) | 元素 | 改变原数组 | 填充 |