接下來的五個方法都是對有序數(shù)組進行的方法函匕,我也會對其中原理進行描述,以便于理解如果對無序數(shù)組進行操作的返回結果毫痕。
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ù)組元素不小于插入元素并且后面的元素遇到比該元素大适室,則將元素插入更大元素的前面,返回當前插入元素的索引值馏艾,不再繼續(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ù)組容为,從傳入元素的結束索引截取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ù)組作為子元素構成的新數(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ù)組方法捋了一遍呜呐,接下來集合的方法就斤。