日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

下划线的学习2

發(fā)布時(shí)間:2025/7/14 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 下划线的学习2 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

對(duì)于上次的內(nèi)容有個(gè)地方值得補(bǔ)充一下:

_[first ? 'find' : 'filter'](obj, function(value) {...

就是這樣的語(yǔ)法,原來(lái)函數(shù)還能這么調(diào)用,用這樣的結(jié)構(gòu),寫(xiě)出來(lái)的代碼豈不是可以動(dòng)態(tài)遞歸自身了么?

從這里開(kāi)始加速度

invoke_.invoke(list, methodName, [*arguments])?
Calls the method named by?methodName?on each value in the?list. Any extra arguments passed to?invoke?will be forwarded on to the method invocation.

_.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); => [[1, 5, 7], [1, 2, 3]]

這就是一反射功能,同時(shí)還結(jié)合了函數(shù)式的集合.通過(guò)方法名,參數(shù)來(lái)調(diào)用.并作用于list.目測(cè)這功能非常強(qiáng)大.非常動(dòng)態(tài).這豈不是意味著可以輕易在在html的大堆標(biāo)記里,弄上一套DSL語(yǔ)言么.

pluck_.pluck(list, propertyName)?
A convenient version of what is perhaps the most common use-case for?map: extracting a list of property values.

var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.pluck(stooges, 'name'); => ["moe", "larry", "curly"]

這就是一投影功能,相當(dāng)于Sql的select,linq的匿名對(duì)象.不過(guò)這個(gè)pluck名字有點(diǎn)生,要我說(shuō), 不如用select,其實(shí)可以考慮定義一個(gè)select別名.

max_.max(list, [iterator], [context])?
Returns the maximum value in?list. If?iterator?is passed, it will be used on each value to generate the criterion by which the value is ranked.

var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.max(stooges, function(stooge){ return stooge.age; }); => {name : 'curly', age : 60};

min_.min(list, [iterator], [context])?
Returns the minimum value in?list. If?iterator?is passed, it will be used on each value to generate the criterion by which the value is ranked.

var numbers = [10, 5, 100, 2, 1000]; _.min(numbers); => 2

sortBy_.sortBy(list, iterator, [context])?
Returns a sorted copy of?list, ranked in ascending order by the results of running each value through?iterator. Iterator may also be the string name of the property to sort by (eg.?length).

_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); }); => [5, 4, 6, 3, 1, 2]

groupBy_.groupBy(list, iterator, [context])?
Splits a collection into sets, grouped by the result of running each value throughiterator. If?iterator?is a string instead of a function, groups by the property named byiterator?on each of the values.

_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); }); => {1: [1.3], 2: [2.1, 2.4]}_.groupBy(['one', 'two', 'three'], 'length'); => {3: ["one", "two"], 5: ["three"]}
很強(qiáng)大的分組功能.

countBy_.countBy(list, iterator, [context])?
Sorts a list into groups and returns a count for the number of objects in each group. Similar to?groupBy, but instead of returning a list of values, returns a count for the number of values in that group.

_.countBy([1, 2, 3, 4, 5], function(num) {return num % 2 == 0 ? 'even' : 'odd'; }); => {odd: 3, even: 2}

shuffle_.shuffle(list)?
Returns a shuffled copy of the?list, using a version of the?Fisher-Yates shuffle.

_.shuffle([1, 2, 3, 4, 5, 6]); => [4, 1, 6, 3, 5, 2]

混亂:把list打亂

toArray_.toArray(list)?
Converts the?list?(anything that can be iterated over), into a real Array. Useful for transmuting the?arguments?object.

(function(){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4); => [2, 3, 4]

和大多數(shù)做法一樣,通常用來(lái)處理參數(shù)

size_.size(list)?
Return the number of values in the?list.

_.size({one : 1, two : 2, three : 3}); => 3

總結(jié)

以上是生活随笔為你收集整理的下划线的学习2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。