陪你读一遍loadash(三)

作者: 小熊猫猫 | 来源:发表于2016-10-03 10:00 被阅读657次
接下来的五个方法都是对有序数组进行的方法,我也会对其中原理进行描述,以便于理解如果对无序数组进行操作的返回结果。

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.

使用二分查找来确定传入的值插入数组的最小索引。
简而言之这个过程就是用待插入的元素与数组元素进行比较,如果遇到数组元素不小于插入元素并且后面的元素遇到比该元素大,则将元素插入更大元素的前面,返回当前插入元素的索引值,不再继续比较,如果数组所有元素比待插入的元素值要大,则返回0,相反,如果数组所有元素比待插入的元素值要小,则返回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.

这个方法类似数组中的indexof()方法。从前往后遍历数组返回,第一个匹配上元素的索引,如果没有匹配到则返回-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方法,与之不同的是遍历数组会从后往前进行遍历。规则和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方法,与之不同的是遍历数组会从后往前进行遍历。规则和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.    

  对有序的数组进行去重。如果传入无序数组则返回原数组。

   例:_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.

返回数组除了第一个元素之外的的所有元素。

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

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

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

创建一个数组,从传入元素的开始索引截取n个元素赋给新数组,返回新数组。

例:_.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.

创建一个数组,从传入元素的结束索引截取n个元素赋给新数组,返回新数组。

例:_.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.

将传入数组进行合并,并对该数组进行去重。

例:_.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对传入数组进行过滤,过滤掉数组中的value值,返回新数组。

_.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.

   对两个数组合并去掉两个数组共有的元素,返回新数组。

   _.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:将传入所有数组的第一个元素放到第一个数组,所有第二个元素放到第二个数组,以此类推。返回一个由这些数组作为子元素构成的新数组。_.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.

将两个数组合并为一个对象,第一个数组的值为对象的键,第二个为对象的值。

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

三篇博客把lodash的数组方法捋了一遍,接下来集合的方法。

相关文章

  • 陪你读一遍loadash(三)

    1 _.sortedIndex(array , value) 例:_.sortedIndex([1,2,3]...

  • Lodash

    Loadash是一个现代实用的JS库,loadash中除了提供一些常用的便捷方法以外还提供了跟函数式编程相关的方法...

  • 读2019-01-10

    读书法 写一遍,读三遍。 写一遍,读三遍。 再一遍,读三遍。 最后一遍加变化。

  • 陪你读一遍loadsh (四)

    Collection方法# 1..countBy(collection, [iteratee=.identity]...

  • 陪你读一遍lodash(一)

    在平常开发中,大家一定会对数组,类数组,对象等数据结构进行复杂处理。lodash提供了强大的功能帮助你解决这些难题...

  • 陪你读一遍lodash(二)

    13、_.fromPairs(pairs) 14、_.head(array) 15、_.indexOf(array...

  • React native 常用组件-优化

    常用三方组件: loadash -- 低版本系统 一些函数不支持.2.performance-now -- ...

  • 2018-2-7日志

    星期三 晴 最近读《王阳明心学》有的文章读一遍就要花10多分钟我没有耐心,读原文读一遍根本读...

  • Loadash 限制操作频率

    看Vue文档时(Vue 侦听器,看到这个,感觉挺有用,记录下,直接上代码。

  • 《日有所诵》二年级下第十三单元通讲记录

    《晨读对韵(二十五)》 教师范读一遍,生齐读一遍,打节奏读一遍。 《晨读对韵(二十六)》 教师范读一遍,生齐读一遍...

网友评论

本文标题:陪你读一遍loadash(三)

本文链接:https://www.haomeiwen.com/subject/rtddyttx.html