gpt4 book ai didi

javascript - 如何通过组变量更改 JSON 结构?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:53:03 24 4
gpt4 key购买 nike

<分区>

我正在尝试更改 JSON 结构以推送到我的数据库

我的旧 JSON:

       [
{"name": "nameGallery"},
{"img": "1.jpg"},
{"img": "2.img"}
]

我想像这样将“img”变量分组到“图像”数组:

[
{
"name": "nameGallery",
"Images": [
{"img": "1.jpg"},
{"img": "2.img"}
]
}
]

我正在尝试使用 object.assign 来管理它,但我不知道为什么会出错。

function getData() {
fetch('text/upload.json').then((res) => res.json())
.then((data) => {
console.log(data);
data = data.map(o => Object.assign({}, o,
{ Images: o.Images.map(({ img }) => ({ img: img })) }
));
})
}

我的结果:

enter image description here

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com