![](/img/trans.png)
[英]What is the best way to check if an objectid exists in an array field that holds all the objectids within a document?
[英]What is the fastest way to make objectIds unique in an Array?
在数组中使objectId唯一的最快方法是什么? 为了测试2个objectId是否相等,是否可以将objectId转换为其他类型,例如数字或字符串?
最简单的方法是将id添加为对象的键,如下所示:
var array = [1, 2, 3, 4, 5, 6, 2, 3, 4]
var obj = {}
array.forEach(function(id){obj[id] = true})
array = Object.keys(obj)
其他替代方法是对数组进行排序,使用二进制搜索以及使用某些数据结构(例如AVL树)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.