How to use the reduce function from underscore

Find comprehensive JavaScript underscore.reduce code examples handpicked from public code repositorys.

367
368
369
370
371
372
373
374
375
376
//ar = _.shuffle(ar)


ar = _.sortBy(ar, (r) => {return - Number(r.probability || 0) })

var total = _.reduce(ar, function(sum, r){ 
    return sum + Number(r.probability || 0) 
}, 0)

if (total <= 0) return ar[f.rand(0, ar.length - 1)]
fork icon33
star icon58
watch icon7

+ 2 other calls in file

318
319
320
321
322
323
324
325
326
327
    const foundDie = allDice.find((die) => die.uuid === dieId);
    return foundDie;
}

getAllDice() {
    const dieUpgrades = _.reduce(
        this.cardsInPlay,
        (dice, card) => {
            return dice.concat(card.dieUpgrades);
        },
fork icon9
star icon10
watch icon1

+ 3 other calls in file

377
378
379
380
381
382
383
384
385
386
query.deleted = false;
query.authComplete = true;
query.type = UserType.Regular;
return new Promise(async (resolve, reject) => {
    const userPaths = _.pickSchema(userModel);
    const projection = _.reduce(
        userPaths,
        (accum, val) => {
            if (!/\./.test(val)) {
                accum[val] = 1;
fork icon0
star icon2
watch icon0

794
795
796
797
798
799
800
801
802
803
  return _.filter(array, function(value){ return !!value; });
};

// Return a completely flattened version of an array.
_.flatten = function(array, shallow) {
  return _.reduce(array, function(memo, value) {
    if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
    memo[memo.length] = value;
    return memo;
  }, []);
fork icon0
star icon0
watch icon1

+ 20 other calls in file

101
102
103
104
105
106
107
108
109
110
} else {
    paths = path.split(/[\.\[]/);
    nPath = _.first(paths);
}

remainingPath = _.reduce(_.rest(paths), function(result, item) {
    if (!_.isEmpty(item)) {
        if (item.match(/^\d\]/)) {
            item = "[" + item;
    }
fork icon0
star icon0
watch icon1

+ 5 other calls in file