Array.prototype.find()
方法返回数组中满足提供的判定函数的第一个成员。
语法:
arr.find( callback [, thisArg ] )
类型声明:
interface Array<T> {find<S extends T>(predicate: (this: void, value: T, index: number, obj: T[]) => value is S,thisArg?: any): S | undefined;find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined;}
参数说明:
参数 | 说明 | 类型 |
---|---|---|
callback | 用于判定数组成员的回调函数 | function |
thisArg | 执行回调函数的 this 值 |
callback
函数的参数:
currentValue
:当前数组中处理的元素index
:数组中正处理的当前元素的索引array
:被调用的数组返回值:
当遍历到的数组成员通过回调函数的判定时,返回数组中该成员,否则返回 undefined
。
let foo = [{ name: 'a', quantity: 2 },{ name: 'b', quantity: 0 },{ name: 'c', quantity: 5 },];const getFoo = (key) => (arr) => arr.name === key;console.log(foo.find(getFoo('c')));// { name: 'c', quantity: 5 }
function isPrime(element, index, array) {let start = 2;while (start <= Math.sqrt(element)) {if (element % start++ < 1) {return false;}}return element > 1;}console.log([4, 6, 8, 12].find(isPrime));// undefined, not foundconsole.log([4, 5, 8, 12].find(isPrime));// 5
当在回调中删除数组中的一个值时,当访问到这个位置时,其传入的值时 undefiend
。
// Declare array with no element at index 2, 3 and 4var a = [0, 1, , , , 5, 6];// Shows all indexes, not just those that have been assigned valuesa.find(function (value, index) {console.log('Visited index ' + index + ' with value ' + value);});// Shows all indexes, including deleteda.find(function (value, index) {// Delete element 5 on first iterationif (index == 0) {console.log('Deleting a[5] with value ' + a[5]);delete a[5]; // 注:这里只是将a[5]设置为undefined,可以试试用a.pop()删除最后一项,依然会遍历到被删的那一项}// Element 5 is still visited even though deletedconsole.log('Visited index ' + index + ' with value ' + value);});