如何在javascript中为每个循环创建
我有如下json数据:如何在javascript中为每个循环创建,javascript,Javascript,我有如下json数据: { "s:Envelope": { "s:Body": { "ExecuteSnapshotRequestsResponse": { "ExecuteSnapshotRequestsResult": { "a:SnapshotResponseItemBase": [ {
{
"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"
}
}
]
}
}
}
}
}
[['vod','1238.4','9.25','38823.49'],['@AAPL','39566.2','1.44','957814.5']]
我希望得到如下输出:
{
"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"
}
}
]
}
}
}
}
}
[['vod','1238.4','9.25','38823.49'],['@AAPL','39566.2','1.44','957814.5']]
我尝试了这一点,但仅在单个阵列中获得输出:
设arr=[]
totalRows.forEach(v => {
arr.push(v["a:Instrument"]["_text"]);
v["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map((e, i) => {
arr.push(e["a:Value"]["_text"]);
});
});
但我希望每个a:SnapshotResponseItemBase都有多个数组,并得到a:Instrument和a:Value
更新:
我已经像这样修复了它,但是这个可以优化吗
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"]);
}
}
试试这个,伙计
arr = totalRows.map(element => {
let one = element["a:Instrument"]["_text"];
let two = element["a:DataTypeValues"]["a:DataTypeResponseValueBase"].map(subArrayElement => {
return subArrayElement["a:Value"]["_text"];
})
return [...one, ...two];
})
console.log(arr);
顺便提一下,为什么您希望它是一个数组数组,而不是一个数组字典,其中键是vod或@AAPL,值是数字。arr.push…v[a:DataTypeValues][a:DataTypeResponseValueBase]。map e=>e[a:Value][\u text]@stackErr现有应用程序就是这样理解数据的