我在React js组件中有一个API调用,该调用返回一个JSON对象。 以下是JSON响应:
[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
现在,对于每个日期,我都希望根据Type(i.e. 'C' or 'D')
组成一个数组,并包含所有成功,失败和Data array
的信息,但是我无法通过Details
。
基本上,我希望每个日期都这样一个数据数组:
[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
以下是我尝试过的内容:
var final=[];
for (const item of data){
final.push(item.Details);
}
console.log(final);
});
这不会返回带有日期的数组。
我尝试了foreach
循环,但这似乎不起作用。
知道如何实现吗?
答案 0 :(得分:2)
let input=[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
let output =input.map((item)=>{
return {"date":item.date,"Details":item.Details}
});
console.log(output)
答案 1 :(得分:0)
var obj = [
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
var output = []
obj.forEach(elem => {
elem.Details.forEach( det => {
if(det.Type === "D")
output.push( {"date": elem.date, "Details": det} )
})
})
console.log(output);
这是您所需要的吗?