2017-04-03 34 views
1

阵列我有对象的一个​​这样的数组:映射通过对象在JavaScript

[{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ] 

现在我想计算该阵列的购买的总和和卖侧和用于每一侧上的平均价格总和。更具体地讲,我想我的输出是这样的: 购买= 3 AvgPrice用于购买= 120 卖= 1个AvgPrice为卖出= 200 我是新来的JavaScript和反应让我有些困难。谢谢!:)

+2

你尝试过这么远吗?听起来像你想使用'reduce' :) –

+0

这与React FYI无关。 – Scimonster

+0

我使用foreach并减少但即时获得一些难度 – user7334203

回答

1

使用Array#forEachObject.keys()简单的解决方案。

var arr = [{side: 'Buy', price: 100}, {side: 'Buy', price: 110}, {side: 'Sell', price: 200}, {side: 'Buy', price: 150}], obj = {}, res = []; 
 

 
arr.forEach(function(v){ 
 
(obj[v.side] || (obj[v.side] = [])).push(v.price); 
 
}); 
 
var res = Object.keys(obj).reduce(function(s,a) { 
 
    s[a] = obj[a].length; 
 
    s['avg' + a] = obj[a].reduce((a,b) => a + b)/obj[a].length; 
 
    return s; 
 
}, {}); 
 
console.log(res);

+0

我们可以将输出值添加到单个对象吗?大答案BTW – user7334203

+0

一样的obj = {旁白:4,bSide:8,aPrice:123,bPrice:12} – user7334203

+1

@ user7334203我会尽量 –

1

使用任何迭代器一样forEach, for etc迭代的数据,并使用两个object变量,一个将存储buy数据和其他将存储sell数据。

写这样的:

var data = [{side: 'Buy', price: 100}, {side: 'Buy', price: 110}, {side: 'Sell', price: 200}, {side: 'Buy', price: 150} ]; 
 

 
var buy = {}, sell = {}; 
 

 
data.forEach(el => { 
 
    if(el.side == 'Buy'){ 
 
      buy['count'] = (buy['count'] || 0) + 1; 
 
      buy['total'] = (buy['total'] || 0) + el.price; 
 
    }else{ 
 
     sell['count'] = (sell['count'] || 0) + 1; 
 
     sell['total'] = (sell['total'] || 0) + el.price; 
 
    } 
 
}) 
 

 
console.log(buy.total/buy.count, sell.total/sell.count);

1

请检查:

var arrayBuySell=[{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ] 
var averageBuy = 0; 
var averageSell = 0; 
var totalbuy=0; 
var totalbuycount=0; 
var totalsellcount=0; 
var totalsell=0; 
for (var i = 0; i < arrayBuySell.length; i++) { 
    if(arrayBuySell[i]="Buy") 
     { 
     totalbuy+=arrayBuySell[i].price; 
     totalbuycount=arrayBuySell[i].price.Count(); 
     } 
    else 
     { 
     totalsell+=arrayBuySell[i].price; 
     totalsellcount=arrayBuySell[i].price.Count(); 
     } 
    } 
averageBuy =totalbuy/totalbuycount; 
averageSell=totalsell/totalsellcount; 
2

可能比较容易只是遍历数组,并保持滚动平均值为每side

var data = [{side: 'Buy', price: 100}, {side: 'Buy', price: 110}, {side: 'Sell', price: 200}, {side: 'Buy', price: 150} ]; 

var tally = { Buy: { count: 0, total: 0 }, Sell: { count: 0, total: 0 } }; 

data.forEach(item => { 
    tally[item.side].count = tally[item.side].count + 1; 
    tally[item.side].total = tally[item.side].total + item.price; 
}) 

console.log("AvgPrice for Buy = " + (tally.Buy.total/tall.Buy.count) + " Sell = 1 AvgPrice for Sell = " + (tally.Sell.total/tall.Sell.count)); 
+0

我觉得这是最理解的解决方案 – user7334203

+0

只是为了澄清,你是硬编码整个'tally'变量,如果有什么会比只是'Buy'和'Sell'元素更为你只写他们下降的手吗?遗憾的是它不会有效@Kinduser没有1点进行超动态代码时要求不要求它。当谈到工作,但我同意,现在是因为更少的代码更易读。 –

+0

对于购买和销售而言,不太可能有太多需要添加的方面,所以对它们进行硬编码对于未来的可维护性是有意义的。您的例子也可以工作,但是需要一段时间才能了解他们是否回来了它在几年的时间(或者如果新开发的项目开始)。 –

0

你可以这样说:

let arr = [{side: "Buy", price: 100}, {side: "Buy", price: 110}, {side:  "Sell", price: 200}, {side: "Buy", price: 150} ]; 
let buyPrices = []; 
let sellPrices = []; 
arr.forEach((item, index) => { 
    if (item.side === 'Buy') { 
    buyPrices.push(item.price); 
    } else if (item.side === 'Sell') { 
    sellPrices.push(item.price); 
    } 
}); 

let avgBuy = buyPrices.reduce((a, b) => a + b)/buyPrices.length; 
console.log(`Buy -> ${buyPrices.length} 
AvgBuyPrices -> ${avgBuy}`); 
let avgSell = sellPrices.reduce((a, b) => a + b)/sellPrices.length; 
console.log(`Sell -> ${sellPrices.length} 
AvgSellPrices -> ${avgSell}`);