I have json data like this:
{
"s:Envelope": {
"s:Body": {
"ExecuteSnapshotRequestsResponse": {
"ExecuteSnapshotRequestsResult": {
"a:SnapshotResponseItemBase": [
{
"a:AdditionalDetails": {},
"a:Error": {},
"a:Tag": {
"_text": "0~##~vod~##~PI,DY,MV"
},
"a:Instrument": {
"_text": "vod"
},
"a:InstrumentDisplay": {},
"a:Currency": {
"_text": "£ "
},
"a:DataTypeValues": {
"a:DataTypeResponseValueBase": [
{
"a:DataType": {
"_text": "PI"
},
"a:DataTypeDisplayName": {
"_text": "PRICE INDEX"
},
"a:Value": {
"_text": "1238.4"
}
},
{
"a:DataType": {
"_text": "DY"
},
"a:DataTypeDisplayName": {
"_text": "DIVIDEND YIELD"
},
"a:Value": {
"_text": "9.25"
}
},
{
"a:DataType": {
"_text": "MV"
},
"a:DataTypeDisplayName": {
"_text": "MARKET VALUE"
},
"a:Value": {
"_text": "38823.49"
}
}
]
},
"a:Date": {
"_text": "2019-04-17T00:00:00"
}
},
{
"a:AdditionalDetails": {},
"a:Error": {},
"a:Tag": {
"_text": "0~##~@AAPL~##~PI,DY,MV"
},
"a:Instrument": {
"_text": "@AAPL"
},
"a:InstrumentDisplay": {},
"a:Currency": {
"_text": "U$"
},
"a:DataTypeValues": {
"a:DataTypeResponseValueBase": [
{
"a:DataType": {
"_text": "PI"
},
"a:DataTypeDisplayName": {
"_text": "PRICE INDEX"
},
"a:Value": {
"_text": "39566.2"
}
},
{
"a:DataType": {
"_text": "DY"
},
"a:DataTypeDisplayName": {
"_text": "DIVIDEND YIELD"
},
"a:Value": {
"_text": "1.44"
}
},
{
"a:DataType": {
"_text": "MV"
},
"a:DataTypeDisplayName": {
"_text": "MARKET VALUE"
},
"a:Value": {
"_text": "957814.5"
}
}
]
},
"a:Date": {
"_text": "2019-04-17T00:00:00"
}
}
]
}
}
}
}
}
I would like to get output like this:
[['vod','1238.4','9.25','38823.49'],['@AAPL','39566.2','1.44','957814.5']]
I tried this but I am getting output in single array only:
let arr = [];
totalRows.forEach(v => {
arr.push(v["a:Instrument"]["_text"]);
v["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map((e, i) => {
arr.push(e["a:Value"]["_text"]);
});
});
but I want multiple array for each a:SnapshotResponseItemBase and get a:Instrument and a:Value
Update:
I have fixed it like this but can this be optimized ?
let arr = [];
for (var i = 0; i < totalRowsCount; i++) {
arr.push(totalRows[i]["a:Instrument"]["_text"]);
for (var j = 0; j < totalRows[i]["a:DataTypeValues"]["a:DataTypeResponseValueBase"].length; j++) {
arr.push(totalRows[i]["a:DataTypeValues"]["a:DataTypeResponseValueBase"][j]["a:Value"]["_text"]);
}
}
vodor@AAPLand the values are the numbers.arr.push(...v["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map( e => e["a:Value"]["_text"]))