讲讲那些数组的API方法

200 阅读3分钟

写在前面

今天是丧丧的小王,但是不能拖更。糊弄也得写呐(不是),等我写够好多好多篇,我就成大牛啦。

前面都是废话,今天讲数组!......的API方法们,嘿嘿。

1、every()

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



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

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

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

2、filter()

filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。


const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

3、find()

find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

const array1 = [5, 12, 8, 130, 44];
 
const found = array1.find(element => element > 10);
 
console.log(found);
// expected output: 12

4、findIndex()

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

const array1 = [5, 12, 8, 130, 44];
 
const isLargeNumber = (element) => element > 13;
 
console.log(array1.findIndex(isLargeNumber));
// expected output: 3

5、pop()

pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
 
console.log(plants.pop());
// expected output: "tomato"
 
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
 
plants.pop();
 
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]

6、push()

push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。

const animals = ['pigs', 'goats', 'sheep'];
 
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
 
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

7、shift()

shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。

const array1 = [1, 2, 3];
 
const firstElement = array1.shift();
 
console.log(array1);
// expected output: Array [2, 3]
 
console.log(firstElement);
// expected output: 1

8、unshift()

unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。

const array1 = [1, 2, 3];
 
console.log(array1.unshift(4, 5));
// expected output: 5
 
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]

9、join()

join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。

const elements = ['Fire', 'Air', 'Water'];
 
console.log(elements.join());
// expected output: "Fire,Air,Water"
 
console.log(elements.join(''));
// expected output: "FireAirWater"
 
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

10、lastIndexOf()

lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
 
console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
 
console.log(animals.lastIndexOf('Tiger'));
// expected output: 1

11、some()

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

const array = [1, 2, 3, 4, 5];
 
// checks whether an element is even
const even = (element) => element % 2 === 0;
 
console.log(array.some(even));
// expected output: true

12、slice()

slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
 
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
 
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
 
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
 
console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]
 
console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]

13、map()

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

const 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]

14、reverse()

reverse() 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。

const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"]
 
const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]
 
// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// expected output: "array1:" Array ["three", "two", "one"]

15、toString()

toString() 返回一个字符串,表示指定的数组及其元素。

const array1 = [1, 2, 'a', '1a'];
 
console.log(array1.toString());
// expected output: "1,2,a,1a"

写在最后

啊我终于c,啊不,写完啦,明天见朋友们,今天也是勤劳的小王,干杯~