2017-04-03 105 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}`);