本文整理匯總了TypeScript中underscore.iteratee函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript iteratee函數的具體用法?TypeScript iteratee怎麽用?TypeScript iteratee使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了iteratee函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: fn
export function sum<T>(list: T[], summer: _.ListIterator<T, number>): number {
const fn = _.iteratee(summer) as _.ListIterator<T, number>;
return _.reduce(
list,
(memo, val, index) => {
return memo + fn(val, index, list);
},
0
);
}
示例2: getItemAcrossStores
/**
* Find an item among all stores that matches the params provided.
*/
function getItemAcrossStores(params: { id?: string; hash?: number; notransfer?: boolean }) {
const predicate = _.iteratee(_.pick(params, 'id', 'hash', 'notransfer')) as (DimItem) => boolean;
for (const store of _stores) {
const result = store.items.find(predicate);
if (result) {
return result;
}
}
return undefined;
}
示例3: sum
export function count<T>(list: T[], predicate: _.ListIterator<T, any>): number {
const fn = _.iteratee(predicate) as _.ListIterator<T, any>;
return sum(list, (item, index) => {
return fn(item, index, list) ? 1 : 0;
});
}
示例4: pick
import * as _ from 'underscore'
export const ModelMixin = {
pick( ...args : any[] ){
return _.pick( this, args );
},
escape( attr ){
return _.escape( this[ attr ] );
},
matches( attrs ){
return !!_.iteratee( attrs, this )( this );
},
omit( ...keys : string[] ) : {} {
return this.mapObject( ( value, key ) => {
if( keys.indexOf( key ) < 0 ){
return value;
}
});
},
invert(){
const inverted = {};
this.each( ( value, key ) => inverted[ value ] = key );
return inverted;
},
pairs(){
return this.map( ( value, key ) => [ key, value ] );