gpt4 book ai didi

javascript - 如何添加新 key :value pair to an existing Javascript object dynamically within a loop

转载 作者:行者123 更新时间:2023-12-02 22:14:26 25 4
gpt4 key购买 nike

我想根据两个对象计算一个新值,并将结果作为新对象添加到现有数组中。

输入内容如下:

[{
"trades": [
{
"fields": {
"orderOpenPrice": "1.40000",
"orderTakeProfit": "1.50000",
[...]

},
},

{
"fields": {
"orderOpenPrice": "1.30000",
"orderTargetPrice": "1.50000",
[...]
}
},
{
"fields": {
"orderOpenPrice": "1.50000",
"orderTargetPrice": "1.55000",
[...]
}
},

[...]

}]

这是所需的输出:

[{
"trades": [
{
"fields": {
"orderOpenPrice": "1.40000",
"orderTakeProfit": "1.50000",
"pipsTargetedKey": "10000",
[...]

},
},

{
"fields": {
"orderOpenPrice": "1.30000",
"orderTakeProfit": "1.50000",
"pipsTargetedKey": "20000",
[...]
}
},
{
"fields": {
"orderOpenPrice": "1.50000",
"orderTakeProfit": "1.55000",
"pipsTargetedKey": "5000",
[...]
}
},

[...]

}]

我使用此线程尝试了两种不同的方法:How can I add a key/value pair to a JavaScript object? :

使用分配:

[...]
for (var i = 0; i < tradesTotal; i++) {
pipsTargeted = Math.abs(trades[i].fields.orderOpenPrice - trades[i].fields.orderTakeProfit);
trades[i].fields.assign(trades[i].fields, {pipsTargetedKey: pipsTargeted});
}
[...]


使用点表示法:

[...]
for (var i = 0; i < tradesTotal; i++) {
pipsTargeted = Math.abs(trades[i].fields.orderOpenPrice - trades[i].fields.orderTakeProfit);
trades[i].fields.pipsTargetedKey = pipsTargeted
}
[...]

但是,这两次尝试都没有添加另一个键:值对。

根据要求进行编辑:

tradesTotal = Object.keys(trades).length;

// manipulate trades object
for (var i = 0; i < tradesTotal; i++) {

// format dateTime
trades[i].fields.orderOpenTime = (trades[i].fields.orderOpenTime).replace('T', ' ');
if (trades[i].fields.orderCloseTime !== null)
trades[i].fields.orderCloseTime = (trades[i].fields.orderCloseTime).replace('T', ' ');

// format orderType
if (trades[i].fields.orderType === 0) {
trades[i].fields.orderType = 'Buy'
} else if (trades[i].fields.orderType === 1) {
trades[i].fields.orderType = 'Sell'
} else if (trades[i].fields.orderType === 2) {
trades[i].fields.orderType = 'Buy Limit'
} else if (trades[i].fields.orderType === 3) {
trades[i].fields.orderType = 'Sell Limit'
} else if (trades[i].fields.orderType === 4) {
trades[i].fields.orderType = 'Buy Stop'
} else if (trades[i].fields.orderType === 5) {
trades[i].fields.orderType = 'Sell Stop'
} else if (trades[i].fields.orderType === 6) {
trades[i].fields.orderType = 'Bank Transaction'
}

// calculate R:R and TP + SL in pips and add result to object
if (stopLoss && takeProfit > 0) {
pipsRisked = Math.abs(trades[i].fields.orderOpenPrice - trades[i].fields.orderStopLoss);
pipsTargeted = Math.abs(trades[i].fields.orderOpenPrice - trades[i].fields.orderTakeProfit);
rrRatio = (pipsTargeted / pipsRisked);
trades[i].fields.pipsRiskedKey = pipsRisked;
trades[i].fields.pipsTargetedKey = pipsTargeted;
trades[i].fields.pipsRRKey = rrRatio;
}
}

最佳答案

正如您提到的,trades 是一个数组。当您对数组执行 Object.keys() 操作时,您将获得该数组的索引。这可以简化为 trades.length 因为它们是相同的东西。

当前,您正在循环交易,这允许您访问数组中的每个对象。每个对象都有一个带有数组的 trades 属性,您也需要对其进行循环。这意味着您需要一个嵌套循环。一种是循环遍历较大对象中的所有对象,另一种循环遍历 trades 属性数组中的所有对象。这可以像这样完成:

const tradesTotal = [{
"trades": [{
"fields": {
"orderOpenPrice": "1.40000",
"orderTakeProfit": "1.50000",


},
},

{
"fields": {
"orderOpenPrice": "1.30000",
"orderTargetPrice": "1.50000",

}
},
{
"fields": {
"orderOpenPrice": "1.50000",
"orderTargetPrice": "1.55000",

}
},
]
}];


for (var i = 0; i < tradesTotal.length; i++) {
var trades = tradesTotal[i].trades;
for (var j = 0; j < trades.length; j++) {
var pipsTargeted = Math.abs(trades[j].fields.orderOpenPrice - trades[j].fields.orderTakeProfit);
trades[j].fields.pipsTargetedKey = pipsTargeted
}

}
console.log(tradesTotal);

关于javascript - 如何添加新 key :value pair to an existing Javascript object dynamically within a loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com