成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

JavaScript 新功能:findLast() 和 findLastIndex()

開(kāi)發(fā) 前端
Wenlu Wang 和 Daniel Rosenwasser 關(guān)于findLast() 和 findLastIndex() 的 ECMAScript 提案解決了這一問(wèn)題。

今天來(lái)看一個(gè) ECMAScript 提案:findLast() 和 findLastIndex()。

提案原因

在 JavaScript 中,可以通過(guò) find() 和 findIndex() 查找數(shù)組中的值。不過(guò),這些方法從數(shù)組的開(kāi)始進(jìn)行遍歷:

const array = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];

array.find(elem => elem.v > 3); // {v: 4}
array.findIndex(elem => elem.v > 3); // 3

如果要從數(shù)組的末尾開(kāi)始遍歷,就必須反轉(zhuǎn)數(shù)組并使用上述方法。這樣做就需要一個(gè)額外的數(shù)組操作。

基本使用

幸運(yùn)的是,Wenlu Wang 和 Daniel Rosenwasser 關(guān)于findLast() 和 findLastIndex() 的 ECMAScript 提案解決了這一問(wèn)題。該提案的一個(gè)重要原因就是:語(yǔ)義。

它們的用法和find()、findIndex()類(lèi)似,只不過(guò)是從后向前遍歷數(shù)組,這兩個(gè)方法適用于數(shù)組和類(lèi)數(shù)組。

  • findLast() 會(huì)返回第一個(gè)查找到的元素,如果沒(méi)有找到,就會(huì)返回 undefined;
  • findLastIndex() 會(huì)返回第一個(gè)查找到的元素的索引。如果沒(méi)有找到,就會(huì)返回 -1;
const array = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];

array.findLast(elem => elem.v > 3); // {v: 5}
array.findLastIndex(elem => elem.v > 3); // 4
array.findLastIndex(elem => elem.v > 5); // -1

簡(jiǎn)單實(shí)現(xiàn)

下面來(lái)簡(jiǎn)單實(shí)現(xiàn)一下這兩個(gè)方法。

  • findLast()
function findLast(arr, callback, thisArg) {
for (let index = arr.length - 1; index >= 0; index--) {
const value = arr[index];
if (callback.call(thisArg, value, index, arr)) {
return value;
}
}
return undefined;
}

const array = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];
findLast(array, elem => elem.v > 3, array) // {v: 5}
findLast(array, elem => elem.v > 5, array) // -1
  • findLastIndex()
function findLastIndex(arr, callback, thisArg) {
for (let index = arr.length - 1; index >= 0; index--) {
const value = arr[index];
if (callback.call(thisArg, value, index, arr)) {
return index;
}
}
return -1;
}

const array = [{v: 1}, {v: 2}, {v: 3}, {v: 4}, {v: 5}];
findLastIndex(array, elem => elem.v > 3, array) // 4
findLastIndex(array, elem => elem.v > 5, array) // -1

lodash源碼

下面是 lodash 實(shí)現(xiàn)這兩個(gè)方法的源碼,供大家學(xué)習(xí)!

  • findLast()
import findLastIndex from './findLastIndex.js'
import isArrayLike from './isArrayLike.js'

/**
* This method is like `find` except that it iterates over elements of
* `collection` from right to left.
*
* @since 2.0.0
* @category Collection
* @param {Array|Object} collection The collection to inspect.
* @param {Function} predicate The function invoked per iteration.
* @param {number} [fromIndex=collection.length-1] The index to search from.
* @returns {*} Returns the matched element, else `undefined`.
* @see find, findIndex, findKey, findLastIndex, findLastKey
* @example
*
* findLast([1, 2, 3, 4], n => n % 2 == 1)
* // => 3
*/
function findLast(collection, predicate, fromIndex) {
let iteratee
const iterable = Object(collection)
if (!isArrayLike(collection)) {
collection = Object.keys(collection)
iteratee = predicate
predicate = (key) => iteratee(iterable[key], key, iterable)
}
const index = findLastIndex(collection, predicate, fromIndex)
return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined
}

export default findLast
  • findLastIndex()
import baseFindIndex from './.internal/baseFindIndex.js'
import toInteger from './toInteger.js'

/**
* This method is like `findIndex` except that it iterates over elements
* of `collection` from right to left.
*
* @since 2.0.0
* @category Array
* @param {Array} array The array to inspect.
* @param {Function} predicate The function invoked per iteration.
* @param {number} [fromIndex=array.length-1] The index to search from.
* @returns {number} Returns the index of the found element, else `-1`.
* @see find, findIndex, findKey, findLast, findLastKey
* @example
*
* const users = [
* { 'user': 'barney', 'active': true },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': false }
* ]
*
* findLastIndex(users, ({ user }) => user == 'pebbles')
* // => 2
*/
function findLastIndex(array, predicate, fromIndex) {
const length = array == null ? 0 : array.length
if (!length) {
return -1
}
let index = length - 1
if (fromIndex !== undefined) {
index = toInteger(fromIndex)
index = fromIndex < 0
? Math.max(length + index, 0)
: Math.min(index, length - 1)
}
return baseFindIndex(array, predicate, index, true)
}

export default findLastIndex

可用性

該提案目前處于第 3 階段,提案地址:https://github.com/tc39/proposal-array-find-from-last

此外,Lodash 和 Ramda 等庫(kù)為數(shù)組提供了findLast() 和 findLastIndex() 操作。

目前,在 Safari 15.4 中已經(jīng)支持了這兩個(gè)方法。期待更多瀏覽器支持這兩個(gè)方法!

責(zé)任編輯:武曉燕 來(lái)源: 前端充電寶
相關(guān)推薦

2024-10-21 09:07:52

2024-03-11 14:34:04

JavaScript開(kāi)發(fā)

2023-10-29 00:56:45

MySQL開(kāi)發(fā)者JSON

2011-07-28 14:06:52

XCode XCode 3.2

2022-04-16 12:21:59

XubuntuLinux

2009-06-19 12:53:56

Spring 2.0

2023-04-17 07:32:01

軟件包OpenBSD

2014-12-19 09:53:25

Android 5.1

2022-04-17 18:55:44

KubuntuLinux 發(fā)行版Ubuntu

2023-04-05 19:27:05

Debian

2022-04-25 10:34:16

UbuntuLinux 內(nèi)核穩(wěn)定版本

2022-03-01 09:08:35

Fedora 36GNOMEKDE 流派

2018-01-02 09:06:10

2021-11-26 15:27:23

MetaWorkplaceTeams

2009-09-17 09:39:28

Chrome 3.0谷歌瀏覽器

2012-09-13 11:08:53

IBMdw

2012-07-20 10:21:13

Ubuntu開(kāi)源

2024-04-26 07:36:42

Hudi 1.0數(shù)據(jù)湖倉(cāng)數(shù)據(jù)查詢(xún)

2012-06-15 14:27:08

Opera 12 新功能

2022-04-25 11:16:24

UbuntuLinux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲国产一区在线 | 中文字幕一区二区三区日韩精品 | 久久综合九色综合欧美狠狠 | 国产中的精品av涩差av | 日韩精品一区二区在线 | 国产精品高潮呻吟久久 | 羞羞视频在线观免费观看 | 免费能直接在线观看黄的视频 | 亚洲第一视频网站 | 久久合久久 | 日韩在线视频精品 | 国产精品久久亚洲7777 | www.色53色.com | 久久一视频 | 国产1区 | 蜜桃综合在线 | 可以在线看的黄色网址 | 91麻豆精品国产91久久久久久 | 欧美中文字幕一区二区 | 午夜精品一区二区三区在线观看 | 色秀网站| 久久激情网 | 亚洲综合色 | 国产精品伦理一区二区三区 | av片在线观看 | 免费在线播放黄色 | 色综合久久久久 | 亚洲日本欧美日韩高观看 | 亚洲一区日韩 | 在线免费观看一区二区 | 日日久 | 久久com | 欧美日韩第一页 | 奇米在线 | 欧美综合一区二区三区 | 美女久久久久久久久 | k8久久久一区二区三区 | 色爱综合网 | 欧美精品网站 | 亚洲成人免费av | 久久久久国产一区二区三区四区 |