我的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循环,但这似乎不起作用。
知道如何实现吗?

最佳答案

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)

10-05 20:41
查看更多