如何在javascript中推送数组值

时间:2015-04-09 09:59:48

标签: javascript jquery arrays

我的预期输出

var Album = {
    album_desc: AlbumTitle,
    id: 1,
    album_title: 'ss',
    AlbumDescription: 'sdsd',    
    ImageName: 'image.jpg',    
    album_pics: below array
};

我需要像下面一样动态推送数组,上面是我的数组,如何在上传图像时动态使用它?我怎么能在数组中推送数组?有什么建议吗?

"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"
}

我的预期输出:

{
    "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"
    }],            
}

2 个答案:

答案 0 :(得分:1)

你能这样试试吗?它适合我。



var album = {
    "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"
    }, ],            
}

var pic = {
        "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.album_pics.push(pic);

console.log(album);




答案 1 :(得分:0)

尝试这样

 var Album = {
     album_desc: AlbumTitle,
     id: 1,
     album_title: 'ss',
     AlbumDescription: 'sdsd',
     ImageName: 'image.jpg',
     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"
     }]
 };
 var newPics = {
     "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.album_pics.push(newPics);