最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript新功能介紹之findLast()和findLastIndex()

 更新時間:2022年04月12日 14:31:14   作者:CUGGZ  
最近工作中遇到了一個關(guān)于查找數(shù)組里面的目標(biāo)元素的方法,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript新功能之findLast()?和findLastIndex()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

今天來看一個 ECMAScript 提案:findLast() 和 findLastIndex()。

提案原因

在 JavaScript 中,可以通過 find() 和 findIndex() 查找數(shù)組中的值。不過,這些方法從數(shù)組的開始進(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ù)組的末尾開始遍歷,就必須反轉(zhuǎn)數(shù)組并使用上述方法。這樣做就需要一個額外的數(shù)組操作。

基本使用

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

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

  • findLast() 會返回第一個查找到的元素,如果沒有找到,就會返回 undefined;
  • findLastIndex() 會返回第一個查找到的元素的索引。如果沒有找到,就會返回 -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

簡單實現(xiàn)

下面來簡單實現(xiàn)一下這兩個方法。

  • 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 實現(xiàn)這兩個方法的源碼,供大家學(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 等庫為數(shù)組提供了findLast() 和 findLastIndex() 操作。

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

總結(jié)

到此這篇關(guān)于JavaScript新功能介紹之findLast()和findLastIndex()的文章就介紹到這了,更多相關(guān)JS findLast() 和findLastIndex()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文詳解JS與TS的主要區(qū)別

    一文詳解JS與TS的主要區(qū)別

    TypeScript(簡稱 TS)和JavaScript(簡稱 JS)都是用于編寫Web應(yīng)用程序的語言,下面這篇文章主要給大家介紹了關(guān)于JS與TS的主要區(qū)別,需要的朋友可以參考下
    2024-03-03
  • js實現(xiàn)網(wǎng)頁倒計時、網(wǎng)站已運行時間功能的代碼3例

    js實現(xiàn)網(wǎng)頁倒計時、網(wǎng)站已運行時間功能的代碼3例

    這篇文章主要介紹了js實現(xiàn)網(wǎng)頁倒計時、網(wǎng)站已運行時間功能的代碼3例,需要的朋友可以參考下
    2014-04-04
  • 在JavaScript中使用for循環(huán)的方法

    在JavaScript中使用for循環(huán)的方法

    這篇文章主要介紹了如何在JavaScript中使用for循環(huán),通過使用JavaScript?for...in循環(huán),我們可以循環(huán)對象的鍵或?qū)傩裕诘鷮ο髮傩曰蜻M(jìn)行調(diào)試時,它可能很有用,但在迭代數(shù)組或?qū)ο筮M(jìn)行修改時,應(yīng)該避免使用for...in循環(huán),需要的朋友可以參考下
    2022-11-11
  • pace.js和NProgress.js兩個加載進(jìn)度插件的一點小總結(jié)

    pace.js和NProgress.js兩個加載進(jìn)度插件的一點小總結(jié)

    這兩個插件都是關(guān)于加載進(jìn)度動畫的,今天就和大家一起了解下pace.js和NProgress.js兩個加載進(jìn)度插件的一點小總結(jié),感興趣的朋友一起看看吧
    2018-01-01
  • js實現(xiàn)京東快遞單號查詢

    js實現(xiàn)京東快遞單號查詢

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)京東快遞單號查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • axios的簡單封裝以及使用實例代碼

    axios的簡單封裝以及使用實例代碼

    一般我們在做一個大型項目的時候,需要用到很多接口時,我們?yōu)榱朔奖闶褂?就把接口封裝起來,這篇文章主要給大家介紹了關(guān)于axios簡單封裝以及使用的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • 禁止頁面刷新讓F5快捷鍵及右鍵都無效

    禁止頁面刷新讓F5快捷鍵及右鍵都無效

    禁止頁面刷新讓F5快捷鍵及右鍵都無效,下面有個不不錯的實現(xiàn)方法,大家可以感受下
    2014-01-01
  • es6 filter() 數(shù)組過濾方法總結(jié)

    es6 filter() 數(shù)組過濾方法總結(jié)

    這篇文章主要介紹了es6 filter() 數(shù)組過濾方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • javascript 獲取所有id中包含某關(guān)鍵字的控件的實現(xiàn)代碼

    javascript 獲取所有id中包含某關(guān)鍵字的控件的實現(xiàn)代碼

    獲取某容器控件中id包含某字符串的控件id列表
    2010-11-11
  • 如何利用js實時監(jiān)聽input輸入框值的變化

    如何利用js實時監(jiān)聽input輸入框值的變化

    在做web開發(fā)時候很多時候都需要即時監(jiān)聽輸入框值的變化,以便作出即時動作去引導(dǎo)瀏覽者增強(qiáng)網(wǎng)站的用戶體驗感,這篇文章主要給大家介紹了關(guān)于如何利用js實時監(jiān)聽input輸入框值的變化,需要的朋友可以參考下
    2024-02-02

最新評論

浦东新区| 宁海县| 那曲县| 新邵县| 赣州市| 宝鸡市| 德州市| 南召县| 阿克苏市| 武平县| 新蔡县| 湘潭县| 东莞市| 东至县| 鄱阳县| 巴中市| 宝兴县| 嘉定区| 东山县| 湄潭县| 湖南省| 定远县| 卓资县| 柯坪县| 来宾市| 通河县| 虎林市| 麦盖提县| 时尚| 汉寿县| 扬中市| 常德市| 吴旗县| 溧水县| 当阳市| 浑源县| 厦门市| 革吉县| 德化县| 惠州市| 新疆|