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"
    }],
}
3
  • I need to push album_pics in array how can i do that ? @Rory Mc Commented Apr 14, 2015 at 6:51
  • Do you simply want to add a new element to the album_pics array? Commented Apr 14, 2015 at 6:53
  • check stackoverflow.com/questions/351409/appending-to-array Commented Apr 14, 2015 at 6:54

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.