数组中的特征项

我有一个包含在variables“foundListings.currentimages”下面的图像链接数组。 在前端,用户可以检查“特色”框以select要显示的图像。 所有这一切运作良好。

但是,我不知道如何将foundListings.currentimages下的图像移动到数组的前面。 (如果它在arrays的前面,它的特色。)

我将如何更改代码foundListings.currentimages.splice(index2, 1); 不从数组中删除项目,而是把它放在数组中?

谢谢你的帮助! 🙂

 // 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(); } 

.unshift()后,你从数组中删除它? 我知道这似乎更多的工作,但我真的不能想到任何其他方式重新sorting数组。

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

那应该这样做