数组

集合

  • 对象:属性的集合
  • 数组:值的集合
  • 函数:语句的集合

对象

内置对象:CMAScript语言的标准

  • string
  • Number
  • Boolean
  • Object
  • Math
  • Date
  • RegExp
  • 等等

宿主对象:浏览器

  • window
  • history
  • location
  • scren
  • document
    自定义对象:程序员自己创建的对象

数据类型

原始值
对象

  • 自定义对象

数组是什么

  • 数组是对象
  • 数组是值的集合
  • 数组是编程语言的一种基础数据类型。
    • 数组元素:数组内的每个元素都有一个数值表示的位置,我们称之为索引。
    • 索引:数组内的每个元素都有一个数值表达示的位置,我们称之为索引

创建数组的方法

创建数组方法有四种:数组字面量:使用方括号创建数组。

  • 使用Array构造函数:new + Array()
  • 扩展符
  • 数组方法

数组字面量

数组字面量就是方括号包裹的数组元素,元素之间用逗号分隔。

let empty = []
let array = [1,2,3]
let array = ['a','b','c']
let array = [1,'hello',true]
let array = [···,'hello',]//array.length = 5

稀疏数组
//数组
let num = 10
let array = [num+1,num+2,num+10]
//数组可以嵌套
let arr1 = [1,2,3]
let arr2 = ['a','b',arr1,true]

扩展符

扩展符:...用于调用并展开数组或对象。
用法:在一个数组字面量中包含另一个数组

let a = [1,2,3]
let b = [...a]

数组的读写操作

稀疏数组vs周密数组

数组的长度

Array.length返回数组的长度

 let arr1 = [1,,3,'hello world',true,]
        let arr2 = [,,]
        console.log(arr1.length);//5
        console.log(arr2.length);//2

用法一:查询
用法二:查询数组的最后一个元素
用法三:从后删除数组元素
用法四:创建稀疏数组

添加和删除数组元素

数组的特点

  • 数组是有索引的
    • 每个数组元素2

去重

let arr = [8,94,55,44,66,55,74,44,8]

冒泡排序

let arr = [38,94,29,11,66,55,74,44,8]

迭代器方法

1.forEach()

定义:迭代数组的每一个元素。(对数组的每一个元素都调用一次指定函数)
语法:

 arr.forEach(function(数组元素,数组索引,数组){},this)

参数:

  • Function(){} 必选
    调用函数的时候,可以在函数内传入一下三个参数
    item: 数组中的一个元素元素,必须。
    index: 数组索引,可选。
    arr: 当前数组,可选。
  • this :可选

返回值:undefined

注意:对于没有值的数组元素,不执行forEach() 方法。

示例:给每个数组元素加1

 let arr = [1,2,3,4,5]
    // arr.forEach(function(数组元素,数组索引,数组){},this)
    arr.forEach(function(item,index,arr){
        console.log(item + 1)//[2,3,4,5,6]
    })

2.map()

定义:使用为每个数组元素调用函数的结果创建新数组。

语法:

array.map(function(tiem, index, arr), thisValue)

参数:

  • Function(){} 必选
    调用函数的时候,可以在函数内传入一下三个参数
    item: 数组中的一个元素元素,必须。
    index: 数组索引,可选。
    arr: 当前数组,可选。
  • this :可选

返回值:数组,为原始数组中的每个元素调用函数的结果。

注意:

示例:为数组每一个元素乘2
  let arr = [1,2,3,4,5]
    arr.map(function(tiem){
       console.log( tiem * 2)//246810

    })

3.Filter

定义:用于筛选数组元素,返回符合条件的新数组,略过不符合的元素,不修改原数组

语法:

array.filter(function(item index, arr), thisValue)

参数:

  • Function(){} 必选
    调用函数的时候,可以在函数内传入一下三个参数
    item: 数组中的一个元素元素,必须。
    index: 数组索引,可选。
    arr: 当前数组,可选。
  • this :可选

返回值:包含所有通过测试的数组元素的数组。如果没有元素通过测试,则返回一个空数组。

注意:
示例:筛选出大于或等于3的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.filter(function(item,index,arr){
    return item >= 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ][ 3, 4, 5 ]

4.Find

定义:返回数组中第一个通过测试的元素的值
语法:

array.find(function(currentValue, index, arr), thisValue)

参数:
第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象

第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,就是undefined。
返回值:如果数组中的任何元素通过测试,则返回数组元素值,否则返回 undefined。
注意:

示例:获取到比数组元素3大的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.find(function(item){
    return item > 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] 4

5.Findindex

定义:返回数组中通过条件的第一个元素的索引
语法:
array.findIndex(function(currentValue, index, arr), thisValue)

参数:
第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象

第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,就是undefined。

返回值:
如果数组中的任何元素通过条件,则返回数组元素索引,否则返回 -1。

注意:
示例:获取数组中第一个值大于 3 的元素的索引

 let arr = [1,2,3,4,5]
    let sum = arr.find(function(item){
    return item > 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] 3

6.Every

定义:检验数组中的所有元素是否都通过了设定的条件(被作为函数提供)。every() 方法对数组中存在的每个元素执行一次函数。
语法:

array.every(function(数组元素, index, arr), thisValue)

参数:

Function(){} 必选
调用函数的时候,可以在函数内传入一下三个参数
item: 数组中的一个元素元素,必须。
index: 数组索引,可选。
arr: 当前数组,可选。
this :可选

返回值:*布尔值。如果数组中的所有元素都通过设定的条件,则返回 true,否则返回 false.
注意:
示例:检测数组元素是否都等于0

  let arr = [1,2,3,4,5]
    arr.every(function(tiem){
        console.log(tiem > 0)//true
    })

7.Some

定义:检查数组中的任何元素是否通过特定条件
语法:

array.some(function(currentValue, index, arr), thisValue)

参数:
第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
sum:用于存储数组之和的变量。
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象
第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递
返回值:布尔值。如果数组中的任何元素通过测试,则返回 true,否则返回 false。
注意:
示例:检测数组中有没有比5大的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.some(function(item){
    return item > 5
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] false

8.reduce

定义:接收一个函数作为累加器,数组中的每个值(从左到右)开始增减,最终计算为一个值。
语法:

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

参数:

total 必需。初始值, 或者计算结束后的返回值。
currentValue 必需。当前元素
currentIndex 可选。当前元素的索引
arr 可选。当前元素所属的数组对象。
initialValue 可选。传递给函数的初始值

返回值:返回计算结果
注意: reduce() 对于空数组是不会执行回调函数的。
示例:数组的每一个元素进行相加

 let arr = [1,2,3,4,5]
    let sum = arr.reduce(function(v,item,index,arr){
    return v + item
    })
    console.log(arr,sum)// [1,2,3,4,5] 15

9.reduceRight

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

语法:

array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)

参数:

- total 必需。初始值, 或者计算结束后的返回值。
- currentValue 必需。当前元素
- currentIndex 可选。当前元素的索引
- arr 可选。当前元素所属的数组对象。
- initialValue 可选。传递给函数的初始值

返回值:
注意:reduce() 对于空数组是不会执行回调函数的
示例:从右到左,减去每个数组元素:

let arr = [1,2,3,4,5]
    let sum = arr.reduceRight(function(v,item,index,arr){
    return v - item
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ]-5
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容