gpt4 book ai didi

javascript - 将项目推送到嵌套数组

转载 作者:行者123 更新时间:2023-12-03 10:21:16 25 4
gpt4 key购买 nike

下面是我的数组:

[{
"album_desc": "Test",
"id": 1,
"album_title": "Test",
"ImageName": "image004.png",
"album_pics": [{
"media_type": "image/png",
"last_modified_date": 1428913015000,
"thumnail_pic_loc": "image004.png",
"large_pic_loc": "image004.png",
"filter_type": "image/png",
"pic_id": "d5bd"
}]
}]

我需要将数组的结构更改为如下所示。如何在上传图像时动态使用它?如何将数组插入数组?有什么建议吗?

{
"album_desc": "Album 1",
"id": "399234688",
"album_title": "Album 1",
"album_pics": [{
"media_type": "image",
"last_modified_date": "2015-01-16T00:40:39.071Z",
"thumnail_pic_loc": "3fe2a54346b3d54e-pinaki2.jpg",
"large_pic_loc": "3fe2a54346b3d54e-pinaki2.jpg",
"filter_type": "image/jpeg",
"pic_id": "d5bc"
}, {
"media_type": "image",
"last_modified_date": "2015-01-16T00:40:39.071Z",
"thumnail_pic_loc": "3fe2a54346b3d54e-pinaki3.jpg",
"large_pic_loc": "3fe2a54346b3d54e-pinaki3.jpg",
"filter_type": "image/jpeg",
"pic_id": "d5bd"
}],
}

最佳答案

我的理解是,您想要向 album_pics 数组添加一个新元素。

var myArray = [{"album_desc": "Test",...}]

var newPicture = {"media_type": "image",... }

myArray[0].album_pics.push(newPicture);

myArray 是您的原始数组,newPicture 是您要添加到 album_pics 数组中的图片。在此示例中,我修改了 myArray 的第一个元素,但它可以是任何元素,例如:myArray[5].album_pics.push(newPicture)

关于javascript - 将项目推送到嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620855/

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