Select photo while keep the order

Posted by wong2 on Stack Overflow See other posts from Stack Overflow or by wong2
Published on 2012-09-23T03:08:00Z Indexed on 2012/09/23 3:37 UTC
Read the original article Hit count: 100

Filed under:

I have a list of photo on the page, each have an unique id, user can click on them to toggle select the photo, when they click the submit button, I need to send the array of selected photo ids to the back end, in the order that the photo was selected.

I think that the fastest way to track if a photo is selected is to use an object that use photo id as key, like:

var selected = {
    "6272861": true,
    "6272888": true
}

when the user unselect a photo, I just need to delete selected["6272861"].

But this will ignore the order, if I use an array to keep the selected photos:

var selected = ["6272861", "6272888"];

then when I need to unselect a photo, I have to loop through the array and delete the item.

Is there better ways? thanks.

© Stack Overflow or respective owner

Related posts about JavaScript