[英]Storing key value pair in javascript
Can we store key value pair in javascript in this way?我们可以这样在javascript中存储键值对吗? array variable arr;数组变量 arr;
arr[key1]={value11,value12,value13...}
arr[key2]={value21,value22,value33...}
example:例子:
arr["class"]={8,9,10}
arr["section"]={a,b,c}
and so on...等等...
I am new to javascript.我是 javascript 的新手。
To store a series or ordered list of values that you're going to index numerically, you'd typically use an array .要存储要以数字方式索引的一系列值或有序值列表,通常会使用数组。 So for instance, [8, 9, 10]
is an array with three entries (the numbers 8
, 9
, and 10
).例如, [8, 9, 10]
是一个包含三个条目(数字8
、 9
和10
)的数组。
To store a keyed set of values, you'd usually use an object (if the set of key/value pairs is reasonably consistent and the keys are strings¹) or a Map
(if the set of key/value pairs varies over time, or keys are not strings).要存储一组键控值,您通常会使用 object(如果键/值对组相当一致并且键是字符串¹)或Map
(如果键/值对组随时间变化,或者键不是字符串)。
Example of an object with an array of numbers:带有数字数组的 object 示例:
const obj = {
class: [8, 9, 10]
};
or building it up:或建立它:
const obj = {};
obj.class = [8, 9, 10];
// or
// obj["class"] = [8, 9, 10];
In this case, the key/value pairs are called properties of the object.在这种情况下,键/值对被称为 object 的属性。
Or with a Map
:或使用Map
:
const map = new Map();
map.set("class", [8, 9, 10]);
In this case, the key/value pairs are called entries of the map.在这种情况下,键/值对被称为 map 的条目。
¹ Technically objects can also have properties keyed by Symbols, but that's not usually something you have to deal with when first getting started. ¹ 从技术上讲,对象也可以具有由符号键控的属性,但这通常不是您刚开始时必须处理的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.