数组api
2020-07-21 本文已影响0人
komallqh
常用方法(4321+221)
- 4
push() 返回数组的长度
pop() 返回回最后一个元素
shift() 返回第一个元素
unshift() 返回数组长度 - 3
splice(i,n,m) 可以添加或删除;返回删除的元素
slice(start,end) 返回切出来的数组,原数组不受影响
concat() 返回连接后的新数组;
concat与push区别:如果添加一个数组元素,concat是把数组中每项元素拆分后添加,push是直接将数组整体添加 - 2
reverse() 返回反转后的数组
sort() 改变原数组
let arr = [2,10,6,1,4,22,3]
console.log(arr.sort()) // [1, 10, 2, 22, 3, 4, 6]
let arr1 = arr.sort((a, b) =>a - b)
console.log(arr1) // [1, 2, 3, 4, 6, 10, 22]
let arr2 = arr.sort((a, b) =>b-a)
console.log(arr2) // [22, 10, 6, 4, 3, 2, 1]
console.log(arr) // [22, 10, 6, 4, 3, 2, 1]
- 1
join
arrayObject.join(separator)
- 2
reduce(callback,initivalValue)
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
reduceRight(callback,initialValue)
let arr = [0,1,2,3,4]
let arr1 = arr.reduceRight((preValue, curValue) =>
preValue + curValue
)
console.log(arr1) // 10
- 2
arr.indexOf(查找的项,开始查找的索引)
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
// expected output: -1
arr.lastIndexOf
var animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
console.log(animals.lastIndexOf('Tiger'));
// expected output: 1
- 1
arr.includes() 判断是否有给定的值
let arr = [1,2,3,4,5]
let arr1 = arr.includes(2)
console.log(arr1) // ture
其他方法(53222)
- 5
forEach(callback)
遍历数组,无返回值; callback参数:value--当前索引值;index--索引;array --原数组;不会修改原数组!不会修改原数组!不会修改原数组!
let arr = [1,2,3,4,5]
arr.forEach( (value,index,array)=>{
console.log(`value:${value} index:${index} array:${array}`)
})
// value:1 index:0 array:1,2,3,4,5
// value:2 index:1 array:1,2,3,4,5
// value:3 index:2 array:1,2,3,4,5
// value:4 index:3 array:1,2,3,4,5
// value:5 index:4 array:1,2,3,4,5
let arr = [1,2,3,4,5]
arr.forEach( (value,index,array)=>{
value = value * 2
console.log(`value:${value} index:${index} array:${array}`)
})
console.log(arr)
// value:2 index:0 array:1,2,3,4,5
// value:4 index:1 array:1,2,3,4,5
// value:6 index:2 array:1,2,3,4,5
// value:8 index:3 array:1,2,3,4,5
// value:10 index:4 array:1,2,3,4,5
// [1, 2, 3, 4, 5]
map(callback)
遍历数组,返回一个新数组;不改变原数组;
let arr = [1,2,3,4,5]
arr.map( (value,index,array)=>{
value = value * 2
console.log(`value:${value} index:${index} array:${array}`)
})
console.log(arr)
var array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
console.log(array1); //[1, 4, 9, 16]
arr.forEach()和arr.map()的区别
1. arr.forEach()是和for循环一样,是代替for。arr.map()是修改数组其中的数据,并返回新的数据。
2. arr.forEach() 没有return arr.map() 有return
filter(callback)
let arr = [1,2,3,4,5]
let arr1 = arr.filter( (value, index) => value < 3)
console.log(arr1) // [1, 2]
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
console.log(words); //['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']
every(callback)
数组元素全部满足条件,返回true
let arr = [1,2,3,4,5]
let arr1 = arr.every( (value, index) => value < 3)
console.log(arr1) // false
let arr2 = arr.every( (value, index) => value < 10)
console.log(arr2) // true
function isBelowThreshold(currentValue) {
return currentValue < 40;
}
var array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
some()
let arr = [1,2,3,4,5]
let arr1 = arr.some( (value, index) => value < 3)
console.log(arr1) // true
let arr2 = arr.some( (value, index) => value > 10)
console.log(arr2) // false
var array = [1, 2, 3, 4, 5];
var even = function(element) {
// checks whether an element is even
return element % 2 === 0;
};
console.log(array.some(even));
// expected output: true
- 3
arr.keys()
let arr = [1,2,3,4]
let arr2 = arr.keys()
for (let key of arr2) {
console.log(key); // 0,1,2,3
}
arr.values()
let arr = [1,2,3,4]
let arr1 = arr.values()
for (let val of arr1) {
console.log(val); // 1,2,3,4
}
arr.entries()
let arr = [1,2,3,4]
let arr1 = arr.entries()
for (let e of arr1) {
console.log(e); // [0,1] [1,2] [2,3] [3,4]
}
- 2
arr.findIndex(value,index,array)
let arr = [1,2,3,4,5,2,4]
let arr1 = arr.findIndex((value, index, array) =>value > 3)
console.log(arr1) // 3
arr.find(value,index,array)
let arr = [1,2,3,4,5,2,4]
let arr1 = arr.find((value, index, array) =>value > 2)
console.log(arr1) // 3
- 2
Array.from()------es6
伪数组转化为数组,只要有length就行
let str = '12345'
console.log(Array.from(str)) // ["1", "2", "3", "4", "5"]
let obj = {0:'a',1:'b',length:2}
console.log(Array.from(obj)) // ["a", "b"]
Array.of()------es6
将一组值转化为数组,类似于声明数组
let str = '11'
console.log(Array.of(str)) // ['11']
//等价于
console.log(new Array('11')) // ['11]
//但是new Array()有缺点,就是参数问题引起的重载
console.log(new Array(2)) //[empty × 2] 是个空数组
console.log(Array.of(2)) // [2]
- 2
arr.fill(target,start,end)
使用给定的值填充一个数组,填充完后会该变原来的数组, 会改变原数组; 会改变原数组
let arr = [1,2,3,4,5]
let arr1 = arr.fill(5)
console.log(arr1) // [5, 5, 5, 5, 5]
console.log(arr) // [5, 5, 5, 5, 5];
let arr = [1,2,3,4,5]
let arr2 = arr.fill(5,2)
console.log(arr2) //[1,2,5,5,5]
let arr3 = arr.fill(5,1,3)
console.log(arr3) //[1,5,5,5,5] 因为改变了原数组,所以这里不是[1,5,5,4,5]
arr.copyWidthin()
参数:target(必选) start(可选) end(可选);获取start到end间的元素替换掉数组中target开始对应的项; 会改变原来的数组!会改变原来的数组!会改变原来的数组!
let arr = [1,2,3,4,5,6,7]
let arr1 = arr.copyWithin(1)
console.log(arr1) // [1, 1, 2, 3, 4, 5, 6]
console.log(arr) // [1, 1, 2, 3, 4, 5, 6]
let arr2 = arr.copyWithin(1,2)
console.log(arr2) // [1, 2, 3, 4, 5, 6, 6]
console.log(arr) // [1, 2, 3, 4, 5, 6, 6]
let arr3 = arr.copyWithin(1,2,4)
console.log(arr3) // [1, 3, 4, 4, 5, 6, 6]
不会修改原数组
concat
slice
join
split
toString
valueOf
迭代方法
会修改原数组
push
pop
shift
unshift
reverse
sort
splice
返回新数组
concat
arr.map(callback)
split
将字符串转化为数组
参考:https://www.cnblogs.com/sqh17/p/8529401.html