百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

常用的JS数组对象API

ztj100 2024-11-25 00:39 19 浏览 0 评论

ES5及以前的Api

  1. ECMAScript5为数组定义了5个迭代方法,每个方法接收两个参数, 一个是每项运行的函数,一个是运行该函数的作用域对象(可选项),传入这些方法的函数会接收三个参数:数组项的值,该项在数组中的位置,数组对象本身。
//  1. Array.every()
//  全真为真  
//  示例用途:全选+单选逻辑
const number = [1, 2, 3, 4, 5]
const result = number.every((item,index,array)=>{
  return item > 0
})
console.log(result) // true

//  2. Array.some()
//  一真为真,与every()相反  
//  示例用途:用于检测数组中是否有满足条件的项
const number = [1, 2, 3, 4, 5]
const result = number.some((item,index,array)=>{
  return item > 2
})
console.log(result) // true

//  3. Array.filter()
//  数据筛选,返回 条件成立的项组成的数组
//  示例用途: 可以适用于 前端的搜索功能
const number = [1, 2, 3, 4, 5]
const result = number.filter((item,index,array)=>{
  return item > 2
})
console.log(result) // [3, 4, 5]


//  4. Array.map()
//  对数组中的每一项运行改定的函数,返回每次函数调用的结果组成的数组
//  示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.map((item,index,array)=>{
  return item * index
})
console.log(result) // [0, 2, 6, 12, 20]


//  5. Array.forEach()
//  对数组中的每一项运行改定的函数,这个方法没有返回值
//  示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.forEach((item,index,array)=>{
   item * index
})
console.log(result) // [0, 2, 6, 12, 20]

//  1. Array.every()
//  全真为真  
//  示例用途:全选+单选逻辑
const number = [1, 2, 3, 4, 5]
const result = number.every((item,index,array)=>{
  return item > 0
})
console.log(result) // true

//  2. Array.some()
//  一真为真,与every()相反  
//  示例用途:用于检测数组中是否有满足条件的项
const number = [1, 2, 3, 4, 5]
const result = number.some((item,index,array)=>{
  return item > 2
})
console.log(result) // true

//  3. Array.filter()
//  数据筛选,返回 条件成立的项组成的数组
//  示例用途: 可以适用于 前端的搜索功能
const number = [1, 2, 3, 4, 5]
const result = number.filter((item,index,array)=>{
  return item > 2
})
console.log(result) // [3, 4, 5]


//  4. Array.map()
//  对数组中的每一项运行改定的函数,返回每次函数调用的结果组成的数组
//  示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.map((item,index,array)=>{
  return item * index
})
console.log(result) // [0, 2, 6, 12, 20]


//  5. Array.forEach()
//  对数组中的每一项运行改定的函数,这个方法没有返回值
//  示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.forEach((item,index,array)=>{
   item * index
})
console.log(result) // [0, 2, 6, 12, 20]

  1. 归并方法 reduce()
  2. // 示例:实现数组元素累加求和 const number = [1, 2, 3, 4, 5] const result = number.reduce((prev,next,index,array) => { return prev + next }) console.log(result) // 15 // 示例:实现数组元素累加求和 const number = [1, 2, 3, 4, 5] const result = number.reduce((prev,next,index,array) => { return prev + next }) console.log(result) // 15
  • 回调函数的参数:
  • accumulate: 前一项,
  • currentValue: 后一项,
  • currentIndex: 项的索引(如果有初始值,从0开始,否则从1开始)
  • arry: 数组对象
  1. 语法: array.reduce(callBack(accumulate,currentValue,currentIndex,array),initialValue)
  2. 概念: 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

  3. initialValue: (可选参数)作为归并基础的初始值
  4. reduce() 可以作为一个高阶函数,用于函数的 compose。
  5. 注意: reduce() 对于空数组是不会执行回调函数的。

reduceRight() 方法的功能和 reduce() 功能是一样的, 不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加。

数组变异方法(含义:改变原数组本身,并且不需要重新赋值给原数组)

  //  1.  默认排序顺序为按字母升序
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort()

  console.log(array1) // [11, 2, 3, 4, 5, 6, 7, 8, 9]

  //  2.  若要按照number形式进行排序,需要传一个函数作为参数
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort((a, b)=>{
    // return b - a  // 降序
    return a - b  // 升序
  })

  console.log(array1) // [2, 3, 4, 5, 6, 7, 8, 9, 11]

  //  3.  随机排序
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort((a, b)=>{
    return Math.random() > 0.5 ? -1 : 1
  })

  console.log(array1) // [4, 9, 8, 5, 6, 2, 7, 11, 3]
  //  1.  默认排序顺序为按字母升序
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort()

  console.log(array1) // [11, 2, 3, 4, 5, 6, 7, 8, 9]

  //  2.  若要按照number形式进行排序,需要传一个函数作为参数
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort((a, b)=>{
    // return b - a  // 降序
    return a - b  // 升序
  })

  console.log(array1) // [2, 3, 4, 5, 6, 7, 8, 9, 11]

  //  3.  随机排序
  const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
  array1.sort((a, b)=>{
    return Math.random() > 0.5 ? -1 : 1
  })

  console.log(array1) // [4, 9, 8, 5, 6, 2, 7, 11, 3]
  const array = [1, 2, 3, 4, 5]
  console.log(array.join())     //  1,2,3,4,5
  console.log(array.join(""))   //  12345
  console.log(array.join("-"))  //  1-2-3-4-5
  const array = [1, 2, 3, 4, 5]
  console.log(array.join())     //  1,2,3,4,5
  console.log(array.join(""))   //  12345
  console.log(array.join("-"))  //  1-2-3-4-5
  const array = [1, 2, 3, 4, 5]
  array.reverse()
  console.log(array)  //  [5, 4, 3, 2, 1]
  const array = [1, 2, 3, 4, 5]
  array.reverse()
  console.log(array)  //  [5, 4, 3, 2, 1]
// 1. 传一个参数时,从当前位置开始,一直删除到最后,返回被删除元素所组成的数组
  const array1 = [1, 2, 3, 4, 5]
  const result = array1.splice(1)
  console.log(result) // [2, 3, 4, 5]
  console.log(array1) // [1]

// 2. 传两个参数时,从下标为 1 位置开始删除,删除 2 个 返回被删除元素所组成的数组
  const array2 = [1, 2, 3, 4, 5]
  const result = array2.splice(1 , 2)
  console.log(result) // [2 ,3]
  console.log(array2) // [1 ,4, 5]

// 3. 传三个参数时,从下标为 1 位置开始删除,删除 2 个 
// ,用1替换被删除的元素 返回被删除元素所组成的数组
  const array3 = [1, 2, 3, 4, 5]
  const result = array3.splice(1 , 2 , 1)
  console.log(result) // [2 ,3]
  console.log(array3) // [1, 1, 1, 4, 5]
// 1. 传一个参数时,从当前位置开始,一直删除到最后,返回被删除元素所组成的数组
  const array1 = [1, 2, 3, 4, 5]
  const result = array1.splice(1)
  console.log(result) // [2, 3, 4, 5]
  console.log(array1) // [1]

// 2. 传两个参数时,从下标为 1 位置开始删除,删除 2 个 返回被删除元素所组成的数组
  const array2 = [1, 2, 3, 4, 5]
  const result = array2.splice(1 , 2)
  console.log(result) // [2 ,3]
  console.log(array2) // [1 ,4, 5]

// 3. 传三个参数时,从下标为 1 位置开始删除,删除 2 个 
// ,用1替换被删除的元素 返回被删除元素所组成的数组
  const array3 = [1, 2, 3, 4, 5]
  const result = array3.splice(1 , 2 , 1)
  console.log(result) // [2 ,3]
  console.log(array3) // [1, 1, 1, 4, 5]
  • array.splice(start,deleteCount,items)
    这个方法很 :ox: :beer: 能对数组进行增删改, 返回一个新的数组
  • array.reverse() 倒序操作
  • array.join() 将数组按照特定的连接方式转换为字符串
  • array.push() 在数组末尾添加一个或多个元素,返回数组的长度
  • array.pop() 删除数组末尾的一个元素,返回被删除的元素
  • array.unshift() 在数组头部添加一个或多个元素,返回数组的长度
  • array.shift() 删除数组头部的一个元素,返回被删除的元素
  • array.indexOf() 查找数组元素,找到返回下标,找不到返回 -1
  • array.sort() 数组排序

数组非变异方法 (含义:不改变原数组本身,需要重新赋值给原数组)

  const arr1 = [1, 2, 3]
  const arr2 = [-1, -2, -3]
  const arr3 = [11, 22, 33]
  console.log(arr1.concat(arr2, arr3)) // [1, 2, 3, -1, -2, -3, 11, 22, 33]
  const arr1 = [1, 2, 3]
  const arr2 = [-1, -2, -3]
  const arr3 = [11, 22, 33]
  console.log(arr1.concat(arr2, arr3)) // [1, 2, 3, -1, -2, -3, 11, 22, 33]
  const arr1 = [1, 2, 3, 4, 5, 6]

  //  传两个参数,包括开始,不包括结束
  const result = arr1.slice(0, 5)
  console.log(result) // [1, 2, 3, 4, 5,]

  //  传一个参数,slice(n)---> 从当前项到数组的最后一项截取
  const result = arr1.slice(1)
  console.log(result) // [2, 3, 4, 5, 6]

  //  参数为负数:表示 arr1.length + 负数
  //  或者理解为倒序截取
  const result = arr1.slice(-5, -2)
  console.log(result) //  [2, 3, 4,]

  // 原数组
  console.log(arr1)   //  [1, 2, 3, 4, 5, 6]
  const arr1 = [1, 2, 3, 4, 5, 6]

  //  传两个参数,包括开始,不包括结束
  const result = arr1.slice(0, 5)
  console.log(result) // [1, 2, 3, 4, 5,]

  //  传一个参数,slice(n)---> 从当前项到数组的最后一项截取
  const result = arr1.slice(1)
  console.log(result) // [2, 3, 4, 5, 6]

  //  参数为负数:表示 arr1.length + 负数
  //  或者理解为倒序截取
  const result = arr1.slice(-5, -2)
  console.log(result) //  [2, 3, 4,]

  // 原数组
  console.log(arr1)   //  [1, 2, 3, 4, 5, 6]
  • arr.slice(start, end) 数组截取
  • arr.concat(arr1, arr2) 组合数组

ES6

1、Array.from()

//  概念: 将伪数组转成真正的数组,将伪数组对象或可遍历对象转换为真数组
    const arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3}
    const arr1 = Array.from(arrayLike)
    console.log(arr1)             //  ['a','b','c']

// 当然也可以用Es6之前的方法
    const arr2 = Array.protoype.slice.call(arrayLike)
    console.log(Array.from(arr2)) //  ['a','b','c']
//  概念: 将伪数组转成真正的数组,将伪数组对象或可遍历对象转换为真数组
    const arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3}
    const arr1 = Array.from(arrayLike)
    console.log(arr1)             //  ['a','b','c']

// 当然也可以用Es6之前的方法
    const arr2 = Array.protoype.slice.call(arrayLike)
    console.log(Array.from(arr2)) //  ['a','b','c']

2、Array.of()

//  将一系列数据生成一个数组
    const arr1 =  new Array(3)
    console.log(arr1)   //  [empty × 3]

    const arr2 =  new Array(3,4)
    console.log(arr2)   //  [3,4]

    const arr3 =  Array.of(3)
    console.log(arr3)   //  [3]
//  将一系列数据生成一个数组
    const arr1 =  new Array(3)
    console.log(arr1)   //  [empty × 3]

    const arr2 =  new Array(3,4)
    console.log(arr2)   //  [3,4]

    const arr3 =  Array.of(3)
    console.log(arr3)   //  [3]

3、Array.isArray(obj)

//  判断变量是否为数组
    console.log(Array.isArray([]))              //  true
    console.log(Array.isArray([1]))             //  true
    console.log(Array.isArray(new Array()))     //  true
    console.log(Array.isArray(Array.prototype)  //  true
//  判断变量是否为数组
    console.log(Array.isArray([]))              //  true
    console.log(Array.isArray([1]))             //  true
    console.log(Array.isArray(new Array()))     //  true
    console.log(Array.isArray(Array.prototype)  //  true

4、拓展运算:...

//  数组的展开操作
    const arr1 = [1,2,3]
    const arr2 = [...arr1]
    arr2.push(4)
    console.log(arr2)   //  [1,2,3,4]

//  函数定义的时候 等同于 argument的作用 ...变量名 得到真正的数组
    function foo(...arr) {
      console.log(...a)  //  数组的展开操作 11 22 33
      foo1.apply(null,a)
    }

    function foo1(a,b,c) {
      console.log(a,b,c) //   11 22 33
    }
    foo(11,22,33)
//  数组的展开操作
    const arr1 = [1,2,3]
    const arr2 = [...arr1]
    arr2.push(4)
    console.log(arr2)   //  [1,2,3,4]

//  函数定义的时候 等同于 argument的作用 ...变量名 得到真正的数组
    function foo(...arr) {
      console.log(...a)  //  数组的展开操作 11 22 33
      foo1.apply(null,a)
    }

    function foo1(a,b,c) {
      console.log(a,b,c) //   11 22 33
    }
    foo(11,22,33)

5、Array.find((item,index,arry)=>{})

//  查找,第一个符合条件的数组成员,如果没有找到,返回undefined
    const arr  = [1,2,3,4,5,6]
    const arr1 = arr.find((item,index,arry)=>{
        // return item return  item > 1
    })//  console.log(arr1) // undefinedconsole.log(arr1) // 2
//  查找,第一个符合条件的数组成员,如果没有找到,返回undefined
    const arr  = [1,2,3,4,5,6]
    const arr1 = arr.find((item,index,arry)=>{
        // return item return  item > 1
    })//  console.log(arr1) // undefinedconsole.log(arr1) // 2

6、Array.findIndex((item,index,arry)=>{})

//  查找,第一个符合条件的数组成员,返回其下标,没有找到则返回 -1 
    const arr  = [1,2,3,4,5,6]
    const arr1 = arr.findIndex((item,index,arry)=>{
        // return item return  item > 3
    })//  console.log(arr1) // -1console.log(arr1) // 4
//  查找,第一个符合条件的数组成员,返回其下标,没有找到则返回 -1 
    const arr  = [1,2,3,4,5,6]
    const arr1 = arr.findIndex((item,index,arry)=>{
        // return item return  item > 3
    })//  console.log(arr1) // -1console.log(arr1) // 4

7、Array.includes(searchElement[,fromIndex = 0])

//  概念:找到返回true , 找不到返回 false (可以和数组的filter方法搭配使用)
//  参数:(要找的元素[,开始查找的索引])
//  从该索引处开始查找 searchElement。 如果为负值。则按升序从
//  array.length + fromIndex 的索引开始搜索。默认为0

    const arr  = [1,2,3,4,5,6]

    const arr1 = arr.includes(3)
    console.log(arr1) //  true

    const arr2 = arr.includes(3,-1)
    // 从下标 6 - 1 = 5 开始查找3
    console.log(arr2) //  false

    const arr3 = arr.includes(3,-10)
    // 从下标 6 - 10 = -4 
    // 如果计算出的索引小于 0 , 则整个数组都会被搜索
    console.log(arr3) //  true
//  概念:找到返回true , 找不到返回 false (可以和数组的filter方法搭配使用)
//  参数:(要找的元素[,开始查找的索引])
//  从该索引处开始查找 searchElement。 如果为负值。则按升序从
//  array.length + fromIndex 的索引开始搜索。默认为0

    const arr  = [1,2,3,4,5,6]

    const arr1 = arr.includes(3)
    console.log(arr1) //  true

    const arr2 = arr.includes(3,-1)
    // 从下标 6 - 1 = 5 开始查找3
    console.log(arr2) //  false

    const arr3 = arr.includes(3,-10)
    // 从下标 6 - 10 = -4 
    // 如果计算出的索引小于 0 , 则整个数组都会被搜索
    console.log(arr3) //  true

相关推荐

Win10预览版10532已知问题汇总(微软win11正式版已知问题一览)

IT之家讯微软已向Insider用户推送了Win10预览版10532更新,本次更新对右键菜单、《Windows反馈》应用以及Edge浏览器进行了改进。除此之外还包含一些Bug,汇总如下,有意升级Wi...

Gabe Aul正测试Win10 Mobile 10532,Insider用户还需等

IT之家讯本月中旬微软向Insider用户推送了Win10Mobile预览版10512,该版本修复了一些Bug,增强了系统稳定性,但依然存在一些问题。今天,微软Insider项目负责人GabeAu...

微软开始推送Win10预览版10532快速版更新

8月28日消息,刚才,微软推送了Win10Build10532快速版,修复了之前的Bug,并带来了三项改进。主要来说,这次的更新改进了右键菜单的UI,使其更具Modern风格(见上图)。此外,更新...

Win10预览版10532更新内容大全(windows10更新预览版)

IT之家讯今天凌晨微软向Insider用户推送了Win10预览版10532快速版更新,本次更新主要带来了三处改进,汇总如下:o改进右键菜单,外观更加Modern。这是基于网友要求界面一致的反馈做出...

无法升级Win10预览版10532?也许Hyper-V在搞鬼

根据IT之家网友的反映,安装了微软虚拟机Hyper-V的Win10预览版用户无法成功升级Build10532版本,安装过程中会被要求回滚系统。很多朋友在尝试关闭虚拟机之后重启安装程序,结果仍然无法顺...

Win10预览版10532界面兴起“酷黑”风潮

Win10预览版10532的界面改动还是较为明显的,主要体现在右键菜单上面。总体来看,该版本的右键菜单间距更宽,视觉上更大气,操作上更便于触控。具体来说,任务栏右键菜单的变化最为明显。除了增加选项的宽...

Win10预览版10532上手图集(windows10预览版下载)

IT之家讯8月28日,微软今天推送了Win10预览版10532快速版更新,在该版本中,微软主要是加强细节上调整,并且主要是增强Edge浏览器性能等。在Windows10预览版10532中,微软改进了...

Win10预览版10532上手视频亮点演示

IT之家讯8月28日消息,今天凌晨微软向WindowsInsider快速通道用户推送了Win10预览版10532。在Windows10预览版10532中,微软改进了右键菜单,外观更加现代化。另外还...

第二篇 前端框架Vue.js(vue前端框架技术)

前端三大核心是网页开发的基础,Vue则是基于它们构建的“生产力工具”。通俗理解就是HTML是化妆的工具如眉笔,CSS是化妆品如口红,JavaScript是化妆后的互动,而Vue就是化妆助手。有了化妆工...

基于SpringBoot + vue2实现的旅游推荐管理系统

项目描述...

基于Vue以及iView组件的后端管理UI模板——iview-admin

介绍iView-admin是一套后端管理界面模板,基于Vue2.0,iView(现在为ViewUI)组件是一套完整的基于Vue的高质量组件库,虽然Github上有一套非常火的基于ElementUI...

别再说你会SPA开发了,这5个核心你真的搞懂了吗?

前言此spa非彼spa,不是你所熟知的spa。你所熟知的spa作者肯定是没有你熟悉的。我们这里指的是在前端开发中的一种模型,叫作单页应用程序,顾名思义,就是整个项目只有一个页面,而页面中的内容是动态的...

React.js Top20面试题(react.js中文官网)

概述作为React开发者,对框架的关键概念和原则有扎实的理解是很重要的。考虑到这一点,我整理了一份包含20个重要问题的清单,每个React开发者都应该知道,无论他们是在面试工作还是只是想提高技能。...

美媒:特朗普签署行政令后,FBI又发现约2400份、总计超14000页涉肯尼迪遇刺案文件

来源:环球时报新媒体1月23日特朗普下令公布肯尼迪遇刺案相关机密文件图源:美媒综合福克斯新闻网和Axios网站10日报道,在总统特朗普签署行政令,要求公布“肯尼迪遇刺案”相关政府机密文件之后,美国...

2021 年 Node.js 开发人员学习路线图

Node.js自发布以来,已成为业界重要破局者之一。Uber、Medium、PayPal和沃尔玛等大型企业,纷纷将技术栈转向Node.js。Node.js支持开发功能强大的应用,例如实时追踪...

取消回复欢迎 发表评论: