通过javascript中的对象数组进行映射

时间:2017-04-03 10:16:42

标签: javascript arrays loops object

我有一个像这样的对象数组:

DECLARE @DATE date
SET @date = GETDATE()
SELECT LEFT(DATENAME(MM, @DATE), 3)

现在我想为这个数组计算买入和卖出方面的总和以及每一方的平均价格总和。更具体一点,我希望我的输出是这样的: 购买= 3 AvgPrice for Buy = 120 卖出= 1 AvgPrice for Sell = 200 我是javascript的新手并做出反应,所以我遇到了一些困难。谢谢!:)

5 个答案:

答案 0 :(得分:2)

可能最容易迭代数组并保持每个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)); 的滚动平均值。

having

答案 1 :(得分: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);

答案 2 :(得分: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);

答案 3 :(得分: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;

答案 4 :(得分: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}`);