简体   繁体   English

通过在文档内部数组中键入密钥来访问文档的有效方法?

[英]Efficient way to access document by key in inside array of documents?

In JavaScript, I need an efficient way to access a document in an array that has the following form: 在JavaScript中,我需要一种有效的方式来访问具有以下形式的数组中的文档:

[
    { 
      a : '1',
      b : '2',
      c : '3'
    },
    { 
      a : '4',
      b : '5',
      c : '6'
    },
    {...},
    {...}
]

So, all the documents have the same keys. 因此,所有文档都具有相同的密钥。 If I have the value for a (for example a = 4 ), is there a way to retrieve the document where a = 4 from the array without looping through all elements in the array and performing the check? 如果我对值a (例如a = 4 ),有一个方法来检索其中文档a = 4从阵列不通过所有的元素阵列中的循环和执行所述检查?

With just the data structure you show, there is no way to retrieve the object where the key a is === '4' without some code that loops through the array. 仅使用您显示的数据结构,如果没有一些循环遍历数组的代码,就无法检索键a为=== '4'的对象。 Arrays don't have have any powers to find content in nested objects within them without looping. 数组没有权力在不循环的情况下在嵌套对象中查找内容。

It would be possible to build a separate index of that array or to restructure the data into a different type of data structure that would then allow you to retrieve the desired item without looping, but not as you have the data structured there. 可以为该数组建立​​一个单独的索引,也可以将数据重新构造为另一种类型的数据结构,这将允许您在不循环的情况下检索所需的项,但因为那里没有结构化的数据。

For example, you could loop over the array once and build an index of all the values of a present in the array such that with one access from that index, you could know which array elements contain the desired value of a . 例如,你可以循环阵列上一次和建立的所有值的索引a存在于阵列,使得与来自索引一个接入,则可以知道哪个数组元素包含的所希望的值在a But, you'd have to first build that type of index before you could use it. 但是,您必须先建立该类型的索引,然后才能使用它。 If this was a one time access, that wouldn't save you any time, but if you were going to be looking up values of a over and over again, it could save a lot of time. 如果这是一次访问,那会救不了你任何时间,但如果你要寻找起来的价值观a一遍又一遍,它可以节省大量的时间。 Build the index once, then use it many times to improve the efficiency of finding a specific value. 一次建立索引,然后多次使用以提高查找特定值的效率。


To make a more efficient way of finding data if the array is large, here's a scheme for indexing the data once and then using that index many times after that. 为了更有效地查找数组较大的数据,这是一种对数据进行一次索引然后在此之后多次使用该索引的方案。 This assumes the data is a string or has a non-ambiguous string conversion (which your example fits): 这假定数据是字符串或具有明确的字符串转换(您的示例适合):

 var data = [ { a : '1', b : '2', c : '3', d : '1'}, { a : '4', b : '5', c : '6', d : '1'}, { e : '3', a : '1', c : '5'}, ]; function ArrayIndex(data) { var index = {}; data.forEach(function(obj, i) { Object.keys(obj).forEach(function(key) { var combinedKeyVal = "_" + key + "_" + obj[key]; var slot = index[combinedKeyVal]; if (!slot) { slot = index[combinedKeyVal] = []; } // add this index to the slot array slot.push(i) }); }); this.find = function(key, val) { var combinedKeyVal = "_" + key + "_" + val; return index[combinedKeyVal] || []; } } var index = new ArrayIndex(data); var found = index.find('a', '4'); log(found); found = index.find('d', '1'); log(found); found = index.find('c', '5'); log(found); found = index.find('d', '2'); log(found); // display output in snippet function log(x) { var div = document.createElement("div"); div.innerHTML = JSON.stringify(x); document.body.appendChild(div); } 

You can use the Array.prototype.filter 您可以使用Array.prototype.filter

var document = arr.filter(function(element) {
    return element.a === '4';
}, arr)[0];

console.log(document);

Maybe you should read about binary search algorithm . 也许您应该阅读有关二进制搜索算法的信息 There are many JS implementations on the Internet. 互联网上有许多JS实现。

If you only need to access objects in the array by the 'a' key, and it is possible to restructure your array as a JSON Object, you could do this: 如果您只需要通过'a'键访问数组中的对象,并且可以将数组重新构造为JSON对象,则可以执行以下操作:

var jsonObj = {
   '1': { b: '2', c: '3' },
   '4': { b: '5', c: '6'},
   ....
}

Then you can retrieve the object with the '1' key like this: 然后,您可以使用“ 1”键检索对象,如下所示:

jsonObj['1']

(Note that numbers in JSON as keys aren't valid, so you wouldn't be able to use jsonObj.1) (请注意,JSON中的数字作为键无效,因此您将无法使用jsonObj.1)

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

相关问题 有没有一种有效的方法可以用键访问数组中的对象? - Is there an efficient way to access an object in an array with its key? 访问对象数组中的键、值对的最佳方法? 在 JavaScript 中 - Best way to access key, value pair inside of an array of objects? in javascript 在数组中查找数组的有效方法是什么? - What is the efficient way to find an array inside an array? 按键值重新组合数组中对象的有效方法 - Efficient way to regroup objects in array by key value Mongodb - 如何在第二个文档中按键连接两个文档并将子文档作为数组合并到父文档 - Mongodb - How to Join two documents by key in second document and merge child documents as array to parent document 按一个键对数组进行排序,但按另一个键列出数组的有效方法? - Efficient way to sort an array by one key, but list the array by another key? 在JavaScript中访问多维数组的最有效方法 - Most efficient way to access a multidimensional array in javascript 查找对象内部key:value的首次出现的有效方法 - Efficient way to find first occurrence of key:value inside object 如何通过使用嵌入式文档的值有效地从JSON文档数组中访问JSON文档? - How do I efficiently access JSON documents from an array of JSON documents, by using value of embedded document? 如何通过键访问数组内部的哈希 - How to access a hash inside of an array by key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM