数组常用方法总结

前言

数组的方法总结


 一、可分类数组方法

1.添加类

var fruits=['Apple','Banana']

添加元素到数组末尾:var newArray= fruits.push('Orange')

添加元素到数组的头部:var newArray=fruits.unshift('Strawberry')

2.删除类:

删除末尾元素var last = fruits.pop()

删除数组最前面(头部)的元素:var first = fruits.shift() //["Banana"]

通过索引删除某个元素:var removedItem = fruits.splice(pos, 1)

从一个索引位置删除多个元素:

var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']

var pos = 1, n = 2

var removedItems = vegetables.splice(pos, n)//从索引是pos位置删除n个元素,并返回删除的元素

console.log(vegetables)//["Cabbage", "Carrot"]

console.log(removedItems)//["Turnip", "Radish"]

3.查找类

Array.index.of()方法用于找出某个元素在数组中的索引:

fruits.push('Mango');

var pos=fruits.index.of('Banana')

Array.find() 方法返回数组中满足条件的第一个元素的值,否则返回 undefined

const array=[5,12,8,130,44]

const found=array.find(item=>item>10)

console.log(found)// 12

Array.findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1

const array= [5, 12, 8, 130, 44]

const show=(item)=>item>13

console.log(array.findIndex(show)) //3

Array.indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']

console.log(beasts.indexOf('bison')) //1

Array.lastIndexOf() 方法返回指定元素,在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo']

console.log(animals.lastIndexOf('Dodo')) //3

Array.includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。

const array=[1,2,3]

consloe.log(array.includes(2)) //true

二.其他数组方法

1.Array.forEach()

fruits.forEach(function (item, index, array) {

console.log(item, index); //Apple 0 Banana 1

});

2.Array.slice() 方法返回一个新的数组对象

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']

console.log(animals.slice(2, 4)) //["camel", "duck"]

console.log(animals.slice(2, -1)) //["camel", "duck"] 截取下标是2的,到倒数第一个之前的元素(不包含最后一个)

console.log(animals.slice(-3, -1)) //["camel","duck"] 截取倒数第三个,到倒数第一个之前的元素(不包含最后一个)

console.log(animals.slice(-3) //['camel', 'duck', 'elephant'] 截取最后三个元素

3.Array.from() 方法

Array.from()将对象转为数组:conslo.log(Array.form('foo')) //转换结果是['f','o','o']

Array.from()实现浅拷贝:Array.from([1, 2, 3], x => x + x

Array.from()实现数组去重:

Array.from ()里面部署了很多的接口对象,利用set可以实现数组去重

let str=[ 1,2,3,4,5,1,2,,7,8]

let str1=Array.from(new Set(str) )

new Set 是实现数组去重、Array.from() 把去重后转换为数组

4.Array.isArray()方法 用于确定传递的值是否是一个数组:(instanceof也可检验是否是数组)

Array.isArray([1, 2, 3]) //true;

Array.isArray(undefined) //false

5.Array.of() 方法,将()里的值创建为新数组

Array.of(7) // [7];Array.of(1, 2, 3) //[1,2,3]

Array(7) //[,,,,,] Array.of(7)和Array(7)之间的区别是:前者创建数组,数组里有一个元素7;后者是创建长度是7、值为empty的数组

6.Array.concat()方法,用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组

const array1 = ['a', 'b', 'c'];const array2 = ['d', 'e', 'f'];

const array3 = array1.concat(array2)

console.log(array3)//["a", "b", "c", "d", "e", "f"]

7.Array.every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值.

const show=(item)=>item<40

const array=[1,30,39,29,10,13]

console.log(array.every(show)) //true

8.Array.fill() 方法有三个参数,fill(value,start,end)

value是想要替换的内容、start开始位置(数组下标),可以省略、end替换结束位置(数组下标)如果省略不写就默认数组结束。

const array= [1, 2, 3, 4]

console.log(array.fill(0,2,4)) //[1,2,0,0]

9.Array.filter() 方法创建一个新数组 const words=['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']

const result=words.filter(item=>item.length>6)

console.log(result) //["exuberant", "destruction", "present"]

10.Array.join() 方法将一个数组的所有元素连接成一个字符串并返回这个字符串.

const elements = ['Fire', 'Air', 'Water']

console.log(elements.join()) //"Fire,Air,Water" join后不写默认是,如后边是('')则是将数组连接起来。//FireAirWater

11.Array.map() 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值

const array1 = [1, 4, 9, 16]

const map1 = array1.map(x => x * 2)

console.log(map1) //[2, 8, 18, 32]

12.Array.reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值

const array= [1, 2, 3, 4]

const reducer = (sum,item)=>sum+item

13.Array.reverse() 方法将数组中元素的位置颠倒

const array= ['one', 'two', 'three']

console,log(array.reverse()) //["three", "two", "one"]

14.Array.some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

const array = [1, 2, 3, 4, 5]

const even = (item) => item% 2 === 0

console.log(array.some(even)) //true


总结

尚有不足,持续更新中...

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值