繁体   English   中英

如何在 javascript 中将对象数组转换为具有唯一 ID 的数组?

[英]How can I convert an array of objects to array with a unique id in javascript?

我有一个像这样的 object 数组:

{
    "data": [
        {
            "id": 1,
            "from": "2022-08-01",
            "to": "2022-08-05",
            "description": "test 1",
            "files": [
                {
                    "id": 1,
                    "hospital_name": "hospital 11",
                    "hospital_id": 11,
                    "period_id": 1
                },
                {
                    "id": 2,
                    "hospital_name": "hospital 11",
                    "hospital_id": 11,
                    "period_id": 1
                }
            ]
        },
        {
            "id": 2,
            "from": "2022-08-06",
            "to": "2022-08-10",
            "description": "test 2",
            "files": [
                {
                    "id": 3,
                    "hospital_name": "hospital 12",
                    "hospital_id": 12,
                    "period_id": 2
                },
                {
                    "id": 4,
                    "hospital_name": "hospital 12",
                    "hospital_id": 12,
                    "period_id": 2
                }
            ]
        }
    ]
}

我想将数组转换成这样:

{
    "data": [
        {
            "id": 1, // this is period id
            "hospital_name": "hospital 11",
            "hospital_id": 11,
            "from": "2022-08-01",
            "to": "2022-08-05",
            "description": "test 1"
        },
        {
            "id": 2,
            "hospital_name": "hospital 12",
            "hospital_id": 12,
            "from": "2022-08-06",
            "to": "2022-08-10",
            "description": "test 2"
        }
    ]
}

所以我期待这样的结果

我尝试像这样键入我的代码:


data.flatMap((period) =>
    period.files.map((file) => ({
        id: period.id,
        hospital_name: file.hospital_name,
        hospital_id: file.hospital_id,
        from: period.from,
        to: period.to,
        description: period.description,
    }))
)

但问题是我的代码显示重复的 id

我该如何解决我的问题?

笔记:

每个时期只有一个医院编号

您可以过滤最后一个结果,例如:

 const j = { "data": [{ "id": 1, "from": "2022-08-01", "to": "2022-08-05", "description": "test 1", "files": [{ "id": 1, "hospital_name": "hospital 11", "hospital_id": 11, "period_id": 1 }, { "id": 2, "hospital_name": "hospital 11", "hospital_id": 11, "period_id": 1 } ] }, { "id": 2, "from": "2022-08-06", "to": "2022-08-10", "description": "test 2", "files": [{ "id": 3, "hospital_name": "hospital 12", "hospital_id": 12, "period_id": 2 }, { "id": 4, "hospital_name": "hospital 12", "hospital_id": 12, "period_id": 2 } ] } ] } console.log(j.data.flatMap((period) => period.files.map((file) => ({ id: period.id, hospital_name: file.hospital_name, hospital_id: file.hospital_id, from: period.from, to: period.to, description: period.description, })) ).filter((value, index, self) => index === self.findIndex((t) => ( t.id === value.id ))))

参考:

您不需要 flatMap,只需在周期上运行 map,对于每个周期,选择第一个文件以提取医院 ID 和医院名称,如下所示:

data.map(period => {
  const file = period.files[0];
  return {
    id: period.id,
    hospital_name: file.hospital_name,
    hospital_id: file.hospital_id,
    from: period.from,
    to: period.to,
    description: period.description,
  }
});

由于files数组中的对象是相同的(除了 id),您可以从data数组中解构属性,并且(在其中)每个files数组的第一个 object,并返回一个新的 object。

 const data={data:[{id:1,from:"2022-08-01",to:"2022-08-05",description:"test 1",files:[{id:1,hospital_name:"hospital 11",hospital_id:11,period_id:1},{id:2,hospital_name:"hospital 11",hospital_id:11,period_id:1}]},{id:2,from:"2022-08-06",to:"2022-08-10",description:"test 2",files:[{id:3,hospital_name:"hospital 12",hospital_id:12,period_id:2},{id:4,hospital_name:"hospital 12",hospital_id:12,period_id:2}]}]}; const out = data.data.map(obj => { const { from, to, files: [{ period_id, id, ...rest }], description } = obj; return { id: period_id, from, to, description, ...rest } }); console.log(out);

附加信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM