简体   繁体   English

在数组中使objectId唯一的最快方法是什么?

[英]What is the fastest way to make objectIds unique in an Array?

What is the fastest way to make objectIds unique in an Array? 在数组中使objectId唯一的最快方法是什么? For testing if 2 objectIds are equal, may I convert objectIds to other type, such as numbers or strings? 为了测试2个objectId是否相等,是否可以将objectId转换为其他类型,例如数字或字符串?

The easiest way would be to add the ids as keys of an object like this: 最简单的方法是将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)

Other alternatives would be sorting the array and using binary search and using some data structure like AVL tree. 其他替代方法是对数组进行排序,使用二进制搜索以及使用某些数据结构(例如AVL树)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查包含文档中所有对象ID的数组字段中是否存在对象ID的最佳方法是什么? - What is the best way to check if an objectid exists in an array field that holds all the objectids within a document? 扫描JS数组的最快方法是什么? - What is the fastest way to scan a JS array 从阵列中获取唯一值的最快方法? - Fastest way to get ONLY unique values from array? 获取 javascript 数组中唯一元素计数的最快方法 - Fastest way to get count of unique elements in javascript array 用Javascript中的另一个数组替换数组的一部分的最快方法是什么? - What is the fastest way to replace a section of an array with another array in Javascript? 在 JavaScript 中对大型(ish)数字数组进行排序的最快方法是什么 - What is the fastest way to sort a large(ish) array of numbers in JavaScript 从 object 数组中删除元素的最快方法是什么 - what is fastest way to remove elements from array of object 在数组中找到n个最小数字的最快方法是什么? - What's the fastest way to find the n smallest numbers in an array? 从数组中获取一系列现有成员的最快方法是什么? - What's the fastest way to get a range of existing members from an array? 在 JavaScript 中遍历数组的最快方法是什么? - What's the fastest way to loop through an array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM