日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

下划线的学习2

發布時間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 下划线的学习2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于上次的內容有個地方值得補充一下:

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

就是這樣的語法,原來函數還能這么調用,用這樣的結構,寫出來的代碼豈不是可以動態遞歸自身了么?

從這里開始加速度

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]]

這就是一反射功能,同時還結合了函數式的集合.通過方法名,參數來調用.并作用于list.目測這功能非常強大.非常動態.這豈不是意味著可以輕易在在html的大堆標記里,弄上一套DSL語言么.

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"]

這就是一投影功能,相當于Sql的select,linq的匿名對象.不過這個pluck名字有點生,要我說, 不如用select,其實可以考慮定義一個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"]}
很強大的分組功能.

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]

和大多數做法一樣,通常用來處理參數

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

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

總結

以上是生活随笔為你收集整理的下划线的学习2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。