![](/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.