0

Below is my array:

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

I need to change the structure of the array to what is shown below. How can I use this dynamically while uploading images? How can I push the array in array? Any suggestions?

{
    "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"
    }],
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

2

My understanding is that you want to add a new element to the album_pics array.

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

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

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

myArray is your original array and newPicture is a picture you want to add to the album_pics array. In this example I modify the first element of myArray but it can be any element, for example: myArray[5].album_pics.push(newPicture)

Michał Komorowski
  • 6,198
  • 1
  • 20
  • 24
0

Use Array.push

var arr = [{
    name: "foo",
    age: 35,
    friends: [{
      name: "zee",
      age: 13
    }]
  }];

  arr[0]['friends'].push({
    name: "boo",
    age: 22
  });


  /*
    result 
    [{
      "name": "foo",
      "age": 35,
      "friends": [{
        "name": "zee",
        "age": 13
      }, {
        "name": "boo",
        "age": 22
      }]
    }]
  */

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push

hussam
  • 596
  • 5
  • 12