Skip to content
On this page

数组原生方法

一、数组和字符串的转换方法(不改变原数组)

  • toString() :将数组转换成⼀个字符串;

  • join() :将数组用连接符连接成一个字符串。

    js
    const arr = [1,2,'b','c']
    const change1 = arr.toString()
    console.log(arr,change1)//(4) [1, 2, 'b', 'c'] '1,2,b,c'
    const change2 = arr.join('')
    const change3 = arr.join('*')
    console.log(arr,change2)//(4) [1, 2, 'b', 'c'] '12bc'
    console.log(arr,change3)//(4) [1, 2, 'b', 'c'] '1*2*b*c'
    
    

二、数组尾部操作方法(改变原数组)

  • pop():删除数组最后一个数据;
  • push():把数据加入到数组尾部,可以传入多个参数

三、数组首部操作方法(改变原数组)

  • shift():删除数组第一个数据;
  • unshitf():把数据加入到数组头部,可以传入多个参数

四、重排序的方法(改变原数组)

  • reverse():反转数组

  • sort():排序数组,默认排序顺序为按字母升序。数字排序:

    js
    var arr = [12,40,142,53,74,5]	//升序
    arr.sort(function(a,b) {return a-b})	//[5, 12, 40, 53, 74, 142]
    var arr2 = [13,42,1,54,88,0,32]		//降序
    arr2.sort(function(a,b) {return b-a})	//[88, 54, 42, 32, 13, 1, 0]
    

他们会直接返回数组

五、数组连接的方法(不改变原数组)

  • concat():将数组拼接在一起,返回的是拼接好的数组
js
const letters = ["a", "b", "c"];

const alphaNumeric = letters.concat(1, [2, 3]);

console.log(alphaNumeric);
// results in ['a', 'b', 'c', 1, 2, 3]

六、数组截取浅拷贝方法(不改变原数组)

  • slice( begin, [end] ) :用于截取数组中的一部分数据返回,begin指从第begin位开始,[end]指从第end-1位开始(end的前一位)。
js
const letters = ["a", "b", "c"];
console.log(letters.slice(0,2))		//['a','b'] 截取到序列为2的前一位(b)

七、数组插入方法(改变原数组)

  • splice(开始索引,删除多少个,需要插入的数据):删除数组中若干数据,并选择是否插入新数据,返回的是删除的数据
js
var arr = [1,23,42,636,364,73,87]
console.log(arr.splice(0,2,22,111))	//[1, 23]
console.log(arr)	//[22, 111, 42, 636, 364, 73, 87]

八、查找特性项的索引的方法

  • indexOf():从前面开始查找数据在数组中的索引位置
  • lastIndexOf():从后面开始查找数据在数组中的索引位置

返回的都是第一次出现的数据的索引位置

九、数组迭代方法

  • every() :判断数组是否每一项都满足条件,返回布尔值;

  • some(): 判断数组是否有某一项满足条件返回布尔值

  • filter() :过滤数组(返回过滤后的数组,不影响原数组);

  • map() :映射数组(返回映射后的数组,不影响原数组);

  • forEach() :遍历数组,无返回值。

every()用法:

js
const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// Expected output: true

some()用法:

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

const even = (ele) => ele % 2 === 0;

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

filter()用法:

js
const word = ['aaaa','daffas','agfa','sdsfdsf','ds']

const result = word.filter(item => item.length > 5)

console.log(result)		//["daffas", "sdsfdsf"]

map()用法:

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

const result = arr.map(item => item + 1)

console.log(result)		//[2, 3, 4, 5, 6]

forEach()用法:

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

arr.forEach(ele => console.log(ele))

十、数组归并方法

  • reduce() :接收一个函数将数组元素进行组合,按索引从低到高(从左到右)。
  • reduceRight() :功能和 reduce() 是一样的,不同的是 reduceRight() 从数组的末尾向前将数组中的数据做累加。

使用方法:

js
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
参数描述
function(total,currentValue, index,arr)必需。用于执行每个数组元素的函数。
initialValue可选。传递给函数的初始值
函数参数描述
total必需。初始值, 或者计算结束后的返回值。
currentValue必需。当前元素
currentIndex可选。当前元素的索引
arr可选。当前元素所属的数组对象。

使用例子:

js
var arr = [1, 2, 3, 4];

var sum = arr.reduce(function(pre,cur,index,arr){
    return pre + cur
})

console.log(sum)	//10