1

I have an array of image links contained in variable "foundListings.currentimages" below. On the front end a user can check a "featured" box to select an image to be featured. All of that works well.

However I'm not sure how to move the image found under foundListings.currentimages to the front of the array. (If it's in the front of the array it's featured.)

How would I change the code foundListings.currentimages.splice(index2, 1); to not remove the item from the array but rather put it first in the array?

Thanks for any help! :)

// SELECTING FEATURED IMAGE
// if any images have been selected for feature, --- add it to front of array
if(req.body.feature && req.body.feature.length) {
for(var i = 0; i < req.body.feature.length; i++) {
var index2 = foundListings.currentimages.indexOf(req.body.feature[i]);
foundListings.currentimages.splice(index2, 1);
}
}
foundListings.save();
}
AndrewLeonardi
  • 3,351
  • 9
  • 47
  • 100

1 Answers1

1

.unshift() after you remove it from the array? i know it seems like way more work, but I can't really think of any other way to reorder an array.

var removed = foundListings.currentimages.splice(index2, 1); 
foundListings.currentimages.unshift(removed[0]);

that SHOULD do it

Matt
  • 1,068
  • 6
  • 10