陪你讀一遍loadash(三)

接下來的五個方法都是對有序數(shù)組進行的方法,我也會對其中原理進行描述,以便于理解如果對無序數(shù)組進行操作的返回結(jié)果。

1 _.sortedIndex(array , value)

Uses a binary search to determine the lowest index at which value  should be inserted into array in order to maintain its sort order.

使用二分查找來確定傳入的值插入數(shù)組的最小索引。
簡而言之這個過程就是用待插入的元素與數(shù)組元素進行比較,如果遇到數(shù)組元素不小于插入元素并且后面的元素遇到比該元素大,則將元素插入更大元素的前面,返回當(dāng)前插入元素的索引值,不再繼續(xù)比較,如果數(shù)組所有元素比待插入的元素值要大,則返回0,相反,如果數(shù)組所有元素比待插入的元素值要小,則返回array.length-1。

例:_.sortedIndex([1,2,3],1);//1
_.sortedIndex([1,2,1,3],1);//1
_.sortedIndex([6,6,6],1);//0

2 _.sortedIndexOf(array ,value)

This method is like [_.indexOf])except that it performs a binary search on a sorted array.

這個方法類似數(shù)組中的indexof()方法。從前往后遍歷數(shù)組返回,第一個匹配上元素的索引,如果沒有匹配到則返回-1。

例:_.sortedIndexof([1,2,3],2);//1
_.sortedIndexof([1,2,3,1,2],2);//1
_.sortedIndexof([1,2,3,1,2],6);//-1

3 _.sortedLastIndex(array ,value)

This method is like [_.sortedIndex]() except that it returns the highest index at which value should be inserted into array in order to maintain its sort order.

 這個方法類似上面講過的_.sortedIndex方法,與之不同的是遍歷數(shù)組會從后往前進行遍歷。規(guī)則和sortedIndex方法一樣。

例:_.sortedLastIndex([4,5,5,5,6],5)//4

4 _.sortedLastIndexOf(array,value)

This method is like [_.lastIndexOf] except that it performs a binary search on a sorted array.

 這個方法類似上面講過的_.sortedIndexof方法,與之不同的是遍歷數(shù)組會從后往前進行遍歷。規(guī)則和sortedIndexof方法一樣。

  例:_.sortedLastIndexOf([4,5,5,5,6],5);//3

5 _.sortedUniq(array)

  This method is like [_.uniq] except that it's designed and optimized for sorted arrays.    

  對有序的數(shù)組進行去重。如果傳入無序數(shù)組則返回原數(shù)組。

   例:_sortedUniq([1,2,3,3]);//[1,2,3]
          _.sortedUniq([3,2,1,2,3,1]);//[3,2,1,2,3,1]

6 _.tail(array)

Gets all but the first element of array.

返回數(shù)組除了第一個元素之外的的所有元素。

例:_.tail([1,2,3])//[2,3]

7 _.take(array,[n=1])

Creates a slice of array with n elements taken from the beginning.

創(chuàng)建一個數(shù)組,從傳入元素的開始索引截取n個元素賦給新數(shù)組,返回新數(shù)組。

例:_.tail([1,2,3,4,5],2)//[1,2]
      _.tail([1,2,3,4,5],0)//[]

8 _takeRigth(array,[n=1])

Creates a slice of array with n elements taken from the end.

創(chuàng)建一個數(shù)組,從傳入元素的結(jié)束索引截取n個元素賦給新數(shù)組,返回新數(shù)組。

例:_.takeRigth([1,2,3,4,5]);[5]
       _.takeRight([1,2,3,4,5],2);[4,5]

9 _.union

Creates an array of unique values, in order, from all given arrays using [SameValueZero

]for equality comparisons.

將傳入數(shù)組進行合并,并對該數(shù)組進行去重。

例:_.union([1,2,3],[1,2]);//[1,2,3]  
       _.union([1,2,3,4,5],[5,5,6]);//[1,2,3,4,5,6]

10 _.without(array ,[values])

  Creates an array excluding all given values using [SameValueZero]for equality comparisons.

  通過values對傳入數(shù)組進行過濾,過濾掉數(shù)組中的value值,返回新數(shù)組。

_.without([1,2,3],1);//[2,3]

11 _.xor([arrays])

  Creates an array of unique values that is the [symmetric difference]of the given arrays. The order of result values is determined by the order they occur in the arrays.

   對兩個數(shù)組合并去掉兩個數(shù)組共有的元素,返回新數(shù)組。

   _.xor([1,2,3],[2,3,4]);//[1,2,3,4] 

12 _.zip _.unzip

Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.

 _.zip:將傳入所有數(shù)組的第一個元素放到第一個數(shù)組,所有第二個元素放到第二個數(shù)組,以此類推。返回一個由這些數(shù)組作為子元素構(gòu)成的新數(shù)組。_.unzip()與_.zip()功能相反,

  _.zip([1,2],['a','b'],[true,false]);//[[1,'a',true],[2,'b',false]]
  _.unzip([[1,'a',true],[2,'b',false]]);//[[[1,'a',true],[2,'b',false]]]

13 _.zipObject([props=[]], [values=[]])

 This method is like [_.fromPairs] except that it accepts two arrays, one of property identifiers and one of corresponding values.

將兩個數(shù)組合并為一個對象,第一個數(shù)組的值為對象的鍵,第二個為對象的值。

_.zipObject(['a','b'],[1,2]);//{a:1,b:2}

三篇博客把lodash的數(shù)組方法捋了一遍,接下來集合的方法。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,922評論 0 33
  • FreeCodeCamp - Basic JavaScript 寫在前面: 我曾經(jīng)在進谷前刷過這一套題,不過當(dāng)時只...
    付林恒閱讀 16,579評論 5 28
  • 01 自私、貪婪、欲望充滿著我們的世界,善良仿佛是唯一的凈土,被古往今來的人們所向往和歌頌。殊不知善良恰恰是經(jīng)歷了...
    不再迷離閱讀 488評論 6 2
  • 0.前言 經(jīng)過近幾年的野蠻生長,國內(nèi)的直播市場的激烈廝殺仍在持續(xù)進行中,這塊看起來很大的蛋糕,真正能吃上的人卻不多...
    Starvicky閱讀 4,881評論 3 10
  • 《第一封情書》 下午一點七分 我決定給你寫情書,清早兩點的決定, 現(xiàn)在開始到年底一百封應(yīng)該差不多了, 什么時候給你...
    萊耶爾羅耶爾閱讀 194評論 0 0

友情鏈接更多精彩內(nèi)容