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();
}
1
  • I've not been able to get the answer on that post to work. Commented Nov 6, 2017 at 0:06

1 Answer 1

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

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

4 Comments

Can you show me how I'd do that?
@AndrewLeonardi see my edit.
I honestly did not expect this to work and it worked 100% perfectly. THANK YOU!!!
@AndrewLeonardi hey no problem at all. good luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.