我有一个图像链接数组包含在下面的变量“foundListings.currentimages”中。在前端,用户可以选中“特色”框来选择要特色的图像。所有这些都运作良好。
但是我不确定如何将foundListings.currentimages下找到的图像移动到数组的前面。 (如果它位于阵列的前面,则它是特色的。)
如何更改代码 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() 从数组中删除后?我知道这似乎需要更多工作,但我真的想不出任何其他方法来重新排序数组。
var removed = foundListings.currentimages.splice(index2, 1);
foundListings.currentimages.unshift(removed[0]);
应该这样做
关于arrays - 数组中的特征项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127901/