作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含外汇交易的 JSON 对象,每个交易都具有以下 key:values
[...]
orderType: 0
orderLots: "1.00"
orderSymbol: "AUDCAD"
[...]
我现在想定义一个循环,遍历每个对象并返回主要货币的空头和多头交易量 (EUR|GBP|AUD|CHF|JPY|USD|CAD)
.
每笔交易都包含基础货币在给定的示例中:AUD
和相对货币在给定的示例中:CAD
每笔交易要么是多头(orderTypes 0、2、4
),要么是空头(orderTypes 1、3、5
)
每笔交易都有一个以 orderLots
表示的交易量,其中 1.00 等于 100.000 个交易单位
现在循环的思路如下:
EURUSD、AUDCAD等
)我的问题(请参阅代码片段底部)是我不知道如何根据需要动态选择预定义变量。否则我将不得不设置几十个 else if
语句。
// define variables
var eurVolumeBought = 0
var eurVolumeSold = 0
var usdVolumeBought = 0
var usdVolumeSold = 0
[...]
var chfVolumeBought = 0
var chfVolumeSold = 0
// iterate each trade in returned JSON object
for (var i = 0; i < tradesTotal; i++) {
symbol = trades[i].fields.orderSymbol
// returns e.g. AUD/CAD
symbolBase = symbol.slice(0, 3)
// returns e.g. AUD
symbolCounter = symbol.slice(3, 6)
// returns e.g. CAD
lots = trades[i].fields.orderLots
// returns e.g. 1.00
orderType = trades[i].fields.orderType
// orderTypes 0, 2, 4 are long trades and 1, 3, 5 are short trades accordingly
// check for main pairs where that contain two major currencies
if (symbolBase.match(/^(EUR|GBP|AUD|CHF|JPY|USD|CAD)$/) && symbolCounter.match(/^(EUR|GBP|AUD|CHF|JPY|USD|CAD)$/){
// Create logic for long trades
if (orderType == '0' || orderType == '2' || orderType == '4') {
// >>>> here i run into issues <<<<
// In the example given, we have a AUDCAD long trade of 100.000 units.
// Thus, I want to dynamically pick the audVolumeBought and cadVolumeSold variables
// to increase them accordingly. My foolish approach right now is as follows:
symbolBase = symbolBase.toLowerCase()
(symbolBase + 'volumeBought') = lots * 100.000 // I try to grab audVolumeBought variable here
}
}
编辑@kastenbutts 评论:
结果变量的值将被推送到 chart.JS 图表中。因此,对于每个贸易对象,都会有一个或两个计算,仅此而已。
使用
result[symbolBase + 'volumeBought'] = result[symbolBase + 'volumeBought'] + (lots * 100.000)
返回 NaN
最佳答案
作为第一步,您可以收集对象中每种货币的所有交易:
let result = {}; // Each key in this object will correspond to a currency
for (var i = 0; i < tradesTotal; i++) {
symbol = trades[i].fields.orderSymbol
symbolBase = symbol.slice(0, 3)
symbolCounter = symbol.slice(3, 6)
lots = trades[i].fields.orderLots
orderType = trades[i].fields.orderType
if (symbolBase.match(/^(EUR|GBP|AUD|CHF|JPY|USD|CAD)$/) && symbolCounter.match(/^(EUR|GBP|AUD|CHF|JPY|USD|CAD)$/){
if (orderType == '0' || orderType == '2' || orderType == '4') {
symbolBase = symbolBase.toLowerCase()
if(result.hasOwnProperty(symbolBase + 'volumeBought')) {
result[symbolBase + 'volumeBought'] += lots * 100.000
}
else {
result[symbolBase + 'volumeBought'] = lots * 100.000
}
}
// ... handle short case
}
作为下一步,您需要根据 ChartJs 的要求将数据转换为图表对象。如果你想要一个简单的条形图,你可以这样做:
let data = [];
let label = [];
for(let cur in result) {
label.push(cur);
data.push(result[cur]);
}
let barChart = {
type: 'bar',
data: {
labels: labels,
datasets: [{
data: data
}]
}
}
注意:我不确定这是否完全符合您需要的逻辑。但这可能是一个很好的起点。
关于javascript - 如何在 JS for-if 循环中动态选择预定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59345531/
我是一名优秀的程序员,十分优秀!