通过javascript中的对象数组进行映射
我有这样一个对象数组:通过javascript中的对象数组进行映射,javascript,arrays,loops,object,Javascript,Arrays,Loops,Object,我有这样一个对象数组: [{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ] 现在我想计算这个数组中买卖双方的总和,以及每一方的平均价格总和。更具体地说,我希望我的输出如下: 购买=3平均购买价格=120 卖出=1平均卖出价=200 我不熟悉javascript,所以我有一些困难。谢谢!:) 使用任何迭代器,如forEach,for etc来
[{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ]
现在我想计算这个数组中买卖双方的总和,以及每一方的平均价格总和。更具体地说,我希望我的输出如下:
购买=3平均购买价格=120
卖出=1平均卖出价=200
我不熟悉javascript,所以我有一些困难。谢谢!:) 使用任何迭代器,如
forEach,for etc
来迭代数据,并使用两个object
变量,一个将存储buy
数据,另一个将存储sell
数据
这样写:
[{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ]
var data=[{side:'Buy',price:100},{side:'Buy',price:110},{side:'Sell',price:200},{side:'Buy',price:150}];
var买入={},卖出={};
data.forEach(el=>{
如果(el.side=='Buy'){
买入['count']=(买入['count']| | 0)+1;
购买['total']=(购买['total']| | 0)+el.price;
}否则{
卖出['count']=(卖出['count']| | 0)+1;
卖出['total']=(卖出['total']| | 0)+el.价格;
}
})
log(buy.total/buy.count,sell.total/sell.count)
使用任何迭代器,如forEach,for etc
来迭代数据,并使用两个object
变量,一个将存储buy
数据,另一个将存储sell
数据
这样写:
[{side: Buy, price: 100}, {side: Buy, price: 110}, {side: Sell, price: 200}, {side: Buy, price: 150} ]
var data=[{side:'Buy',price:100},{side:'Buy',price:110},{side:'Sell',price:200},{side:'Buy',price:150}];
var买入={},卖出={};
data.forEach(el=>{
如果(el.side=='Buy'){
买入['count']=(买入['count']| | 0)+1;
购买['total']=(购买['total']| | 0)+el.price;
}否则{
卖出['count']=(卖出['count']| | 0)+1;
卖出['total']=(卖出['total']| | 0)+el.价格;
}
})
log(buy.total/buy.count,sell.total/sell.count)
使用数组#forEach
和Object.keys()
的简单解决方案
var arr=[{side:'Buy',price:100},{side:'Buy',price:110},{side:'Sell',price:200},{side:'Buy',price:150}],obj={},res=[];
arr.forEach(功能(v){
(obj[v.side]| |(obj[v.side]=[])).push(v.price);
});
var res=Object.keys(obj).reduce(函数s,a){
s[a]=obj[a]。长度;
s['avg'+a]=obj[a]。减少((a,b)=>a+b)/obj[a]。长度;
返回s;
}, {});
控制台日志(res)
使用数组#forEach
和Object.keys()
的简单解决方案
var arr=[{side:'Buy',price:100},{side:'Buy',price:110},{side:'Sell',price:200},{side:'Buy',price:150}],obj={},res=[];
arr.forEach(功能(v){
(obj[v.side]| |(obj[v.side]=[])).push(v.price);
});
var res=Object.keys(obj).reduce(函数s,a){
s[a]=obj[a]。长度;
s['avg'+a]=obj[a]。减少((a,b)=>a+b)/obj[a]。长度;
返回s;
}, {});
控制台日志(res)代码>请检查以下内容:
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;
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;
对于(var i=0;i
请检查以下内容:
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;
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;
对于(var i=0;i
可能最简单的方法是迭代数组,并为每个边保持一个滚动平均值
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));
可能最简单的方法是迭代数组,并为每个边保持一个滚动平均值
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));
您可以这样做:
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}`);
您可以这样做:
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}`);
到目前为止你试过什么?听起来你想使用reduce
:)这与React无关,仅供参考。我使用foreach和reduce,但我遇到了一些困难。到目前为止,你做了哪些尝试?听起来你想使用reduce
:)这与React FYI无关。我使用foreach和reduce,但我遇到了一些困难。我认为这是最容易理解的解决方案。只是澄清一下,你在硬编码整个tally
变量。如果不仅仅是Buy
和Sell
元素会怎么样?你会用手把它们写下来吗?不幸的是,它不能有效地工作,但我同意,现在它的可读性更强了,因为代码更少了。@Kinduser在需求不需要的情况下,编写超级动态代码是没有意义的。当涉及到买卖时,不太可能有太多的方面需要添加,因此对它们进行硬编码对于未来的可维护性是有意义的。你的例子也会起作用,但如果有人在几年后再来(或者如果一个新的开发人员开始这个项目),他们需要花一些时间才能理解。@MichaelPeyper它并不像看上去那么复杂。我也是一个初学者,有5个月的经验。总之,我只是认为,当有人决定就此提出问题时,他希望得到高质量的解决方案,即使他不理解,这也是一种不断学习的动力。我要读一读