2016-08-23 15 views
0

underscorejsを流星上で実行している間、以下の問題に直面しています。テンプレートヘルパーの例外:TypeError:_.mapObjectが関数ではありません

"Exception in template helper: TypeError: _.mapObject is not a function"

助言してください。

var types = _.groupBy(areaFlatten, 'category'); 
     console.log(types); 
     var result = **_.mapObject**(types, function(val, key) { 
     return _.reduce(val, function(memo, v) { 
      return memo + v.val; 
     }, 0)/val.length * 10; 

答えて

2

私はUnderscoreの古いバージョンを使用していると思います。 _.mapObject_.mapObjectを使用せずに(http://underscorejs.org/#changelog

代替v1.8.0で追加されました:あなたが定期的にこの機能を使用しようとしている場合は

var types = _.groupBy(areaFlatten, 'category'); 
console.log(types); 
var result = {}; 
_.each(types, function(val, key) { 
    result[key] = _.reduce(val, function(memo, v) { 
     return memo + v.val; 
    }, 0)/val.length * 10; 
}); 

、あなたが取得するまでの機能を利用できるようにミックスインを追加することができますアップグレードするチャンス、

_.mixin({ 
    mapObject: function(obj, iteratee, context) { 
    var output = {}; 
    _.each(obj, function(v, k) { 
     output[k] = iteratee.apply(context || this, arguments); 
    }); 
    return output; 
    } 
}); 
+0

私はUPDATをした代わりに_.mapObject –

+0

の任意の代替にunderscorejsをアップグレードする方法はこちらをご覧代案を使って答えを編集 – mikeapr4

関連する問題