Typescript/Angular -创建包含父JSON对象特定属性的子数组

xjreopfe  于 2023-03-04  发布在  TypeScript
关注(0)|答案(1)|浏览(127)

我的父对象包含以下数据

[
    {
        "BEGIN_TIME": "24 Feb 2023 13:36",
        "END_TIME": "24 Feb 2023 13:37",
        ...
    },
    {
        "BEGIN_TIME": "24 Feb 2023 13:37",
        "END_TIME": "24 Feb 2023 13:38",
        ...
    },
    {
        "BEGIN_TIME": "24 Feb 2023 13:38",
        "END_TIME": "24 Feb 2023 13:39",
        ...
    }
]

我想创建一个包含父对象特定属性的数组。例如,我需要END_TIME属性的所有值。类似于以下内容

child_array=parent_json.Get(END_TIME);

预期成果

["24 Feb 2023 13:37","24 Feb 2023 13:38","24 Feb 2023 13:39"]
z8dt9xmd

z8dt9xmd1#

使用Map方法:

const parent_json = [
  {
    "BEGIN_TIME": "24 Feb 2023 13:36",
    "END_TIME": "24 Feb 2023 13:37"
  },
  {
    "BEGIN_TIME": "24 Feb 2023 13:37",
    "END_TIME": "24 Feb 2023 13:38"
  },
  {
    "BEGIN_TIME": "24 Feb 2023 13:38",
    "END_TIME": "24 Feb 2023 13:39"
  }
];

const child_array = parent_json.map(obj => obj.END_TIME);

相关问题