简体   繁体   English

如何检查对象数组中是否存在唯一索引

[英]how to check if a unique index exists in an array of objects

I have an array in which should look like this:我有一个数组,其中应如下所示:

const arr = [
 {index:12, data: a},
 {index:15, data: w},
 {index:32, data: e},
 {index:42, data: g}
]

Is it possible to find out if an index exists using the arr.includes() method?是否可以使用arr.includes()方法找出索引是否存在? if yes, how then.如果是,那么如何。 if no, what is the best options to find if an index exists?如果否,查找索引是否存在的最佳选择是什么?

You can use array.some() and pass an arrow function:您可以使用array.some()并传递箭头 function:

 const arr = [ {index:12, data: 'a'}, {index:15, data: 'w'}, {index:32, data: 'e'}, {index:42, data: 'g'} ] let exists = arr.some(x => x.index === 12); console.log(exists);

You can use filter() and you will also recieve array of items with that index:您可以使用filter()并且您还将收到具有该索引的项目数组:

 const arr = [ {index:12, data: 'x'}, {index:15, data: 'x'}, {index:32, data: 'x'}, {index:42, data: 'x'} ]; const targetIndex = 42; const filtered = arr.filter(item=>item.index===targetIndex); console.log(`There ${filtered.length?'is':'is not'} index ${targetIndex} in array`);

Or you can use some()或者你可以使用some()

 const arr = [ {index:12, data: 'x'}, {index:15, data: 'x'}, {index:32, data: 'x'}, {index:42, data: 'x'} ]; const targetIndex = 42; const isThere = arr.some(item=>item.index===targetIndex); console.log(`There ${isThere?'is':'is not'} index ${targetIndex} in array`);

Using includes (or filter , some ) can be expensive.使用includes (或filtersome )可能很昂贵。 If you have a large array (hundreds or more), or you need to determine inclusion often, then it can be very slow.如果您有一个大数组(数百个或更多),或者您需要经常确定包含,那么它可能会非常慢。 That's because these functions loop through the array looking inside of the objects with the function you provide until the outcome is reached.这是因为这些函数循环遍历数组,使用您提供的 function 查找对象内部,直到达到结果。

If performance is a concern you may find that building an indexing object is best for you.如果性能是一个问题,您可能会发现构建索引 object 最适合您。 For example:例如:

const byIndices = {};
arr.forEach(obj => byIndices[obj.index] = obj);
if (byIndices[12]) console.log("item 12 is",byIndices[12].data)

With this approach you loop through the entire array once (the forEach loop) to build the indexing object byIndices , then you can use direct object syntax to not only determine existence, but also get at the object.使用这种方法,您遍历整个数组一次( forEach循环)以构建索引 object byIndices ,然后您可以使用直接 object 语法不仅确定存在,还可以获取 ZA8CFDE6331BD59EB66666F89111C4。

Since Javascript is optimized for working with objects, doing lookups like byIndices[val] are very quick, and since it's all object references it's not too bad for memory use either.由于 Javascript 已针对处理对象进行了优化,因此进行像byIndices[val]类的查找非常快,并且由于它都是 object 引用,因此对于 ZCD69B4957F06CD818D7BF3D61980E291 使用也不算太糟糕。

However for small arrays, or if you only need to find entries occasionally then it's probably simplest to use some or filter to do the job.但是对于小型 arrays,或者如果您只需要偶尔查找条目,那么使用somefilter来完成这项工作可能是最简单的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM