如何在循环中向现有Javascript对象动态添加新的key:value对

如何在循环中向现有Javascript对象动态添加新的key:value对,javascript,Javascript,我想基于两个对象计算一个新值,并将结果作为新对象添加到现有数组中 输入看起来像: [{ "trades": [ { "fields": { "orderOpenPrice": "1.40000", "orderTakeProfit": "1.50000", [...] }, }, { "fi

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

输入看起来像:

[{
    "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",
                [...]
        }
        },

        [...]

}]
[...]
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": [
        {
        "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",
                [...]
        }
        },

        [...]

}]
[...]
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": [
        {
        "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",
                [...]
        }
        },

        [...]

}]
[...]
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": [
        {
        "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",
                [...]
        }
        },

        [...]

}]
[...]
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;
    }
}
tradesTotal=Object.keys(trades).length;
//操纵交易对象
对于(var i=0;i0){
pipsRisked=Math.abs(交易[i].fields.orderOpenPrice-trades[i].fields.orderStopLoss);
pipsTargeted=Math.abs(交易[i].fields.orderOpenPrice-trades[i].fields.orderTakeProfit);
rrRatio=(pipsTargeted/pipsRisked);
交易[i].fields.pipsRiskedKey=pipsRisked;
交易[i].fields.pipsTargetedKey=pipsTargeted;
交易[i].fields.pipsRRKey=rrRatio;
}
}
只需使用函数和:

const交易=[
{
“字段”:{
“orderOpenPrice”:“1.40000”,
“订单利润”:“1.50000”,
},
},
{
“字段”:{
“orderOpenPrice”:“1.30000”,
“订单利润”:“1.50000”,
}
},
{
“字段”:{
“orderOpenPrice”:“1.50000”,
“订单利润”:“1.55000”,
}
}
]
const mappedTrades=trades.map(trade=>{
const{orderOpenPrice,orderTakeProfit}=trade.fields
返回{
……贸易,
字段:{
…贸易领域,
pipsTargetedKey:Math.abs(数字(orderOpenPrice)-数字(orderTakeProfit))
}
}
})

另一种方法是使用类似或的东西。

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

目前,您正在循环进行
交易
,这允许您访问阵列中的每个对象。每个对象都有一个
属性和一个数组,您还需要循环该数组。这意味着您需要一个嵌套循环。一个用于循环较大属性数组中的所有对象,另一个用于循环
交易属性数组中的所有对象。可以这样做:

const tradesTotal=[{
“行业”:[{
“字段”:{
“orderOpenPrice”:“1.40000”,
“订单利润”:“1.50000”,
},
},
{
“字段”:{
“orderOpenPrice”:“1.30000”,
“orderTargetPrice”:“1.50000”,
}
},
{
“字段”:{
“orderOpenPrice”:“1.50000”,
“orderTargetPrice”:“1.55000”,
}
},
]
}];
对于(var i=0;iconsole.log(tradesTotal)
如果这是你的内循环,你需要将
.length
添加到
tradesTotal
@NickParsons tradesTotal是一个分配给对象的变量。length已经存在,这很好。你能添加外循环吗?@NickParsons添加了原始代码,谢谢。
trades
变量是您的对象数组中的属性交易吗?还是你的整个数组输入?@Phanti不用担心。目前,您用于计算
pipsTargetedKey
的公式不正确,因此无法准确给出所需的结果。(有些对象使用
orderTakeProfit
,有些对象使用
orderTargetPrice