javascript - 无法在 Javascript 中从数组获取数组

标签 javascript arrays json

我的 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": []
                    }
                ]
            }
        ]
    }
]

现在,对于每个日期,我想根据类型(即“C”或“D”)形成一个数组,其中包含所有成功、失败和数据数组,但我我无法了解详细信息

基本上,我想要每个日期的数据数组,如下所示:

[
    {
        "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)

关于javascript - 无法在 Javascript 中从数组获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56058103/

相关文章:

javascript - 如何在 Plone 4.1 的 <embed> 标签中使用 atreal.richfile.preview 时禁用 pdf 插件图标的显示

javascript - Angular 返回它自己的标签而不是在 ejs 中输出

java - 将 ArrayList<ArrayList<Object>> 转换为 Object[][]

javascript - 按输入类型对数组和分组进行排序

python - 正确地将 JSON 转换并格式化为 CSV?

javascript - 如何调用 lodash/fp get 并使用默认值?

javascript - 如何在基本构造函数中调用重写的方法

arrays - 具有指定维度的数组的类型别名

json - jq json过滤器并保持原始结构

javascript - list 图标第 1 行不是,列 : 1, 意外标记