我想将此对象简化为包含产品名称和平均价格的对象。什么是最快的方法?
var foo = { group1: [
{
name: "one",
price: 100
},
{
name: "two",
price: 100
}],
group2: [
{
name: "one",
price: 200
},
{
name: "two",
price: 200
}],
group3: [
{
name: "one",
price: 300
},
{
name: "two",
price: 300
}]
}
导致
var foo2 = [{
name: 'one',
price: 200
},{
name: 'two',
price: 200
}];
谢谢!
答案 0 :(得分:20)
在Evan的游行中不要下雨,但是这里有一个更短的选择;)
result = _.chain(original)
.flatten()
.groupBy(function(value) { return value.name; })
.map(function(value, key) {
var sum = _.reduce(value, function(memo, val) { return memo + val.price; }, 0);
return {name: key, price: sum / value.length};
})
.value();
答案 1 :(得分:4)
我真的很喜欢redmallard的解决方案,但我想打一点高尔夫球。
Underscore不包含sum
函数,但我们可以通过添加sum
mixin来编写非常优雅的函数表达式。此函数在underscore-contribs repo中称为add。
然后我们可以写:
// Somewhere in the initialization of the program
_.mixin({
sum : function (arr) {
return _.reduce(arr, function (s, x) { return s + x;}, 0);
}
});
result = _.chain(original)
.flatten()
.groupBy('name') // shorthand notation
.map(function (value, key) {
var sum = _.chain(value).pluck('price').sum().value();
return { name: key, price: sum / value.length};
})
.value();
http://plnkr.co/edit/ul3odB7lr8qwgVIDOtM9
但是我们也可以创建一个avg
mixin来扩展我们的工具带:
// Somewhere in the initialization of the program
_.mixin({
sum : function (arr) {
return _.reduce(arr, function (s, x) { return s + x;}, 0);
},
avg : function (arr) {
return _.sum(arr)/arr.length;
}
});
result = _.chain(original)
.flatten()
.groupBy('name') // shorthand notation
.map(function (value, key) {
return { name: key, price: _.avg(value)};
})
.value();
答案 2 :(得分:0)
编辑:暂时离开这里,但我完全忘记了_.flatten,所以redmallard得到了much better answer。
如果您已经知道产品名称并且它们出现在每个组中,您可以通过这种方式快速完成所有工作:
var productAveragePrices = function ( groups, names ) {
return _.map( names, function ( name ) {
var product = { name: name }, productPricesSum = 0;
_.each( groups, function ( group ) {
productPricesSum += ( _.findWhere( group, product ).price );
});
product.price = productPricesSum / _.size( groups );
return product;
});
};
var foo2 = productAveragePrices = function ( foo, ['one', 'two'] );
我把它放在一起,即使你的小组有不同的产品(例如第一组,第二组和第四组中的“一个”和第一组和第三组中的“两个”),这应该有效:
var productPriceReducer = function( memo, group ) {
_.each( group, function( product ) {
// Grabs the current product from the list we're compiling
var memoProduct = _.findWhere( memo, { name: product.name });
if ( !memoProduct ) {
// If the product doesn't exist, creates a holder for it and its prices
memoProduct = {
name: product.name,
prices: [ product.price ]
};
memo.push( memoProduct );
} else {
// Otherwise, it just adds the prices to the existing holder.
memoProduct.prices.push( product.price );
}
});
return memo;
};
// This gets us a list of products with all of their prices across groups
var productPrices = _.reduce( foo, productPriceReducer, [] );
// Then reducing to the average is pretty simple!
var productAveragePrices = _.map( productPrices, function ( product ) {
var sumPrices = _.reduce( product.prices, function ( memo, price ) {
return memo + price;
}, 0 );
return {
name: product.name,
price: sumPrices / product.prices.length
};
});
你仍然可以在一个带有计数器的功能中对上面的价格进行上述操作,但是这样,你也可以得到价格,比如你想要采用标准偏差或找到模式。