JavaScript新功能介紹之findLast()和findLastIndex()_第1頁
JavaScript新功能介紹之findLast()和findLastIndex()_第2頁
JavaScript新功能介紹之findLast()和findLastIndex()_第3頁
JavaScript新功能介紹之findLast()和findLastIndex()_第4頁
JavaScript新功能介紹之findLast()和findLastIndex()_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第JavaScript新功能介紹之findLast()和findLastIndex()目錄提案原因基本使用簡單實現(xiàn)總結(jié)今天來看一個ECMAScript提案:findLast()和findLastIndex()。

提案原因

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

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

array.find(elem=elem.v//{v:4}

array.findIndex(elem=elem.v//3

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

基本使用

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

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

findLast()會返回第一個查找到的元素,如果沒有找到,就會返回undefined;findLastIndex()會返回第一個查找到的元素的索引。如果沒有找到,就會返回-1;

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

array.findLast(elem=elem.v//{v:5}

array.findLastIndex(elem=elem.v//4

array.findLastIndex(elem=elem.v//-1

簡單實現(xiàn)

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

findLast()

functionfindLast(arr,callback,thisArg){

for(letindex=arr.length-1;indexindex--){

constvalue=arr[index];

if(callback.call(thisArg,value,index,arr)){

returnvalue;

}

returnundefined;

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

findLast(array,elem=elem.v3,array)//{v:5}

findLast(array,elem=elem.v5,array)//-1

findLastIndex()

functionfindLastIndex(arr,callback,thisArg){

for(letindex=arr.length-1;indexindex--){

constvalue=arr[index];

if(callback.call(thisArg,value,index,arr)){

returnindex;

}

return-1;

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

findLastIndex(array,elem=elem.v3,array)//4

findLastIndex(array,elem=elem.v5,array)//-1

lodash源碼

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

findLast()

importfindLastIndexfrom'./findLastIndex.js'

importisArrayLikefrom'./isArrayLike.js'

*Thismethodislike`find`exceptthatititeratesoverelementsof

*`collection`fromrighttoleft.

*@since2.0.0

*@categoryCollection

*@param{Array|Object}collectionThecollectiontoinspect.

*@param{Function}predicateThefunctioninvokedperiteration.

*@param{number}[fromIndex=collection.length-1]Theindextosearchfrom.

*@returns{*}Returnsthematchedelement,else`undefined`.

*@seefind,findIndex,findKey,findLastIndex,findLastKey

*@example

*findLast([1,2,3,4],n=n%2==1)

*//=3

functionfindLast(collection,predicate,fromIndex){

letiteratee

constiterable=Object(collection)

if(!isArrayLike(collection)){

collection=Object.keys(collection)

iteratee=predicate

predicate=(key)=iteratee(iterable[key],key,iterable)

constindex=findLastIndex(collection,predicate,fromIndex)

returnindex-1iterable[iterateecollection[index]:index]:undefined

exportdefaultfindLast

findLastIndex()

importbaseFindIndexfrom'./.internal/baseFindIndex.js'

importtoIntegerfrom'./toInteger.js'

*Thismethodislike`findIndex`exceptthatititeratesoverelements

*of`collection`fromrighttoleft.

*@since2.0.0

*@categoryArray

*@param{Array}arrayThearraytoinspect.

*@param{Function}predicateThefunctioninvokedperiteration.

*@param{number}[fromIndex=array.length-1]Theindextosearchfrom.

*@returns{number}Returnstheindexofthefoundelement,else`-1`.

*@seefind,findIndex,findKey,findLast,findLastKey

*@example

*constusers=[

*

{'user':'barney',

'active':true},

*

{'user':'fred',

'active':false},

*

{'user':'pebbles','active':false}

*findLastIndex(users,({user})=user=='pebbles')

*//=2

functionfindLastIndex(array,predicate,fromIndex){

constlength=array==null0:array.length

if(!length){

return-1

letindex=length-1

if(fromIndex!==undefined){

index=toInteger(fromIndex)

index=fromIndex0

Math.max(length+index,0)

:Math.min(index,length-1)

returnbaseFindIndex(array,predicate,index,true)

export

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論