0

I'm trying to convert this Javascript object:

image_uploads: [
  0: {
    upload_id: 50,
  },
  1: {
    upload_id: 51,
  },
  2: {
    upload_id: 52,
  },
]

Into different entries using this format to be able to send it in the body of a POST request:

image_uploads[0][upload_id] = 50
image_uploads[1][upload_id] = 51
image_uploads[2][upload_id] = 52

Probably needing an array of those entries like:

params: [
  "image_uploads[0][upload_id]": 50,
  "image_uploads[1][upload_id]": 51,
  "image_uploads[2][upload_id]": 52,
]

Thanks!

3
  • 2
    Your javascript object is not an object but a weird hybrid between object and array.. Commented Jul 6, 2018 at 22:21
  • Are you using jQuery? jQuery.param(object) will create that format. Commented Jul 6, 2018 at 22:36
  • @Barmar No, I'm using Angular6 Commented Jul 6, 2018 at 22:41

1 Answer 1

0

If you want to keep appending keys inside an array. You can do that with:

var image_uploads = [];
image_uploads.push({'upload_id': 50});
image_uploads.push({'upload_id': 51});
image_uploads.push({'upload_id': 52});

Then you can access them like so:

image_uploads[0]['upload_id']; // 50
image_uploads[1]['upload_id']; // 51
image_uploads[2]['upload_id']; // 52

For both creating your array and accessing them, you can also use loops.

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

1 Comment

He's trying to create the serialized parameter names to send to a server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.