arrays - 数组中的特征项

标签 arrays node.js mongodb image sorting

我有一个图像链接数组包含在下面的变量“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/

相关文章:

mysql - 执行从 MySql 到 Mongodb 的查询

mongodb - spring data mongodb 按集合查找

java - 如何使用 ByteBuffer 查看 header 偏移量来制作字节数组?

c# - 为什么 .net 中的数组只实现 IEnumerable 而不是 IEnumerable<T>?

java - list.toArray() 同意将 A 类型的项目传输到 B[] 类型的数组

javascript - 一个 grunt 的替代方案——编译 sass 和 minify JS

javascript - 循环遍历json并获取nodejs中的特定值

javascript - 处理 javascript 中跨模块的相对路径

c# - 从 C# 2.1 驱动程序正确关闭 MongoDB 数据库连接?

objective-c - 比较数组中的字符串和另一个字符串