简体   繁体   English

数组是否比在javascript中存储和搜索键值对的对象好?

[英]Is array better than objects for storing and searching key value pairs in javascript?

I am fetching data from db and storing it in an array in key value pair, so that i can get values of respective keys as below 我正在从数据库中获取数据,并将其存储在键值对的数组中,这样我就可以如下获取各个键的值

var test = [];
for ()            //fetching and storing from db
{
    test[key_array[i]] = "value i";
       .
       .
       .   
}


test["id_1"] = "value 1"; //to get the value of id_<number>

we can achive this though objects too. 我们也可以通过对象来达到这个目的。 I wanted to know which is the better option to consider, if we want a fast and optimized code 我想知道如果我们想要一个快速优化的代码,哪个是更好的选择

Is array better than objects for storing and searching key value pairs in javascript? 数组是否比在javascript中存储和搜索键值对的对象好?

No. In fact, you're not using the array as an array, you're using it as an object. 不。实际上,您不是将数组用作数组,而是将其用作对象。 You could literally change your 你可以从字面上改变你的

var test = [];

to

var test = {};
// or
var test = Object.create(null); // To avoid it having a prototype

and it would do the same thing you're seeing now: 它会执行与您现在看到的相同的操作:

 var test = {}; test["id_1"] = "value_1"; console.log(test["id_1"]); var test2 = Object.create(null); test2["id_2"] = "value_2"; console.log(test2["id_2"]); 

(And you should change it, since using an array only for its object features is confusing to people maintaining your code.) (并且您应该更改它,因为仅将数组用于其对象功能会使维护代码的人感到困惑。)

[] is a property accessor that accepts the property name as a string. []是一个属性访问器,它接受属性名称作为字符串。 (We use it with standard arrays as well, using numbers, but in theory they get converted to strings before the property is looked up, because standard arrays aren't really arrays at all ¹.) (我们也将其与使用数字的标准数组一起使用,但是从理论上讲,它们会在查找该属性之前转换为字符串,因为标准数组并不是真正的数组 ¹。)

In ES2015+, of course, you'd probably use a Map , not an object: 当然,在ES2015 +中,您可能会使用Map而不是对象:

 const test = new Map(); test.set("id_1", "value_1"); console.log(test.get("id_1")); 


¹ That's a post on my anemic little blog. ¹这是我贫乏的小博客上的帖子。

Arrays, are just a kind of object specialized for storing sequences of things. 数组只是一种专门用于存储事物序列的对象。 If you evaluate typeof [], it produces "object". 如果评估typeof [],它将产生“对象”。

https://eloquentjavascript.net/04_data.html#h_cqg63Sxe3o https://eloquentjavascript.net/04_data.html#h_cqg63Sxe3o

The idea of an array is store sequences of data with the same type or logic structure. 数组的概念是存储具有相同类型或逻辑结构的数据序列。 Thus, is logic put the data come from a db into an array. 因此,逻辑将数据从db放入数组。

Note that you should avoid this because it can become very confusing : 请注意,您应该避免这种情况,因为它会变得非常混乱:

 var test = []; test["id_1"] = "value_1"; test[-1] = "value_0"; console.log(test[-1],test["id_1"]); console.log(test.length,Array.isArray(test)); 

It lets you think that test is an array (and it is) but test[-1] and test["id_1"] are not part of the array (that's why Array.isArray(test) returns true , but test.length is still equal to 0. See explanation on link provided by mister TJ Crowder 它使您认为test是一个数组(并且确实是),但是test[-1]test["id_1"]不属于数组(这就是Array.isArray(test)返回true ,而test.length是仍等于0。请参阅Mister TJ Crowder提供的链接说明

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

相关问题 过滤搜索所有键值对的对象数组 - Filter an array of objects searching all key-value pairs 在JavaScript中将值数组转换为对象(键值)对数组 - Convert array of values to array of objects (key-value) pairs in JavaScript 在对象数组中搜索匹配的属性和值对 - Searching for matching property and value pairs in an array of objects 通过比较具有不同键值对的对象来对 Javascript 数组进行排序 - Sorting Javascript array by comparing objects with different key-value pairs 如何在JavaScript中将多个对象转换为键值对数组[] - How to convert multiple Objects to an Array [] of key-value pairs in javascript 如何在 JavaScript 中将嵌套对象数组转换为具有键值对的对象 - How to convert an array of nested objects to an object with key, value pairs in JavaScript javascript - 具有键/值对的数组 - javascript - array with key/value pairs 将键/值对数组转换为对象数组 - Transform an array of key/value pairs into array of objects 以对象作为键值对中的值构建数组 - Build Array with Objects as the value in key value pairs 在 nodejs 中执行以下操作的更好方法:比较导入的 csv 或对象数组的行中是否存在特定键值对 - Better way of doing the following in nodejs: Comparing if specific key value pairs exist in rows of a imported csv or array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM