I am trying to fetch a version of an object without the specific details. In other words, I want to retrieve everything except the Trades element in the nested array of objects below.
What would be the most efficient way to achieve this? The list of attributes on each level is much longer than shown here. Is there a method to duplicate everything except one attribute at a certain level or except one level (in this case, keep Trades but remove its Trade node)?
[{
"SP": {
"countOrders": 47,
"count": 0,
"Orders": [{
"81803965": {
"symbol": "RFX",
"description": "REDFLOW LTD",
"Trades": {
"Trade": [{
"conId": "81803965",
"tradeId": "17891517",
"transactionId": "51996490"
I have managed to accomplish this using the following method:
const pfSummary = JSON.parse(JSON.stringify(portfolios));
var key;
pfSummary.forEach(function (pf) {
for (x in pf) {
key = x;
break;
}
pf[key].Orders.forEach(function (or) {
for (x in or) {
key = x;
break;
}
delete or[key].Trades;
});
});
While this solution works, I believe there may be a more effective approach to achieve the same outcome, particularly in how we retrieve an object's key...?
If you have better suggestions or solutions, please share them!
Akabin's revised solution:
pfSummary.forEach(pf => {
let pfKey = Object.keys(pf);
pf[pfKey].Orders.forEach(or => {
let orKey = Object.keys(or);
delete or[orKey].Trades;
});
});