underscore包的使用

写在前面:

这里记录underscore包的一些使用,仅是为了自己记忆……
引用的语句:
var _ = require(‘underscore’);
当然,使用之前应该先安装underscore,使用npm install underscore,这个应该知道的哦!

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

.indexBy.indexBy(list, iteratee, [context])

Given a list, and an iteratee function that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.

var stooges = [{name: ‘moe’, age: 40}, {name: ‘larry’, age: 50}, {name: ‘curly’, age: 60}];
_.indexBy(stooges, ‘age’);
=> {
“40”: {name: ‘moe’, age: 40},
“50”: {name: ‘larry’, age: 50},
“60”: {name: ‘curly’, age: 60}
}

    原文作者:Karrdy
    原文地址: https://segmentfault.com/a/1190000002531414
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞