简体   繁体   English

带有说明的javascript多维数组

[英]javascript multidimensional array with descriptions

I would like to have an array like this: 我想要一个像这样的数组:

array['parameter1'] = 1,2,3,4
array['parameter2'] = A,B,C,D

(simple construction as explanation) (简单的结构作为说明)

And I would like to call it later like: 我想稍后再称它为:

alert(array['parameter2']) to get "A,B,C,D" alert(array['parameter2'])以获得“ A,B,C,D”

At the moment, I have a simple array: 目前,我有一个简单的数组:

var array = [];
$.each($("input[name='checkbox']:checked"), function(){            
   array.push($(this).val());
});

But this doesn't help. 但这无济于事。 Need help please :) 请帮助:)

You don't use associative arrays in Javascript because 您无需在Javascript中使用关联数组,因为

If you use named indexes, JavaScript will redefine the array to a standard object. 如果使用命名索引,JavaScript会将数组重新定义为标准对象。 After that, some array methods and properties will produce incorrect results. 之后,某些数组方法和属性将产生错误的结果。

For that purpose you will use objects. 为此,您将使用对象。

You can write it like this: 您可以这样写:

let obj = {
    parameter1: ["A", "B", "C", "D"],
    parameter2: [1, 2, 3, 4]
}

You can also assign new parameters to your object by saying: 您还可以通过以下方式为对象分配新参数:

obj.parameter3 = [5, 6] //some values in that array.

or 要么

obj["parameter3"] = [5, 6]

If you want to get the value from object, you can access it like this: 如果要从对象获取值,可以像这样访问它:

console.log(obj.parameter1)

Here is how you should initialize your object so that it makes your logic easier. 这是初始化对象的方法,以便使逻辑更容易。

The code snippet here also shows how you can call each array inside the parameter object. 此处的代码段还显示了如何调用参数对象内的每个数组。

 let parameters = { parameter1: [1, 2, 3, 4], parameter2: ['A', 'B', 'C', 'D'] } console.log(parameters) console.log(parameters['parameter1']) console.log(parameters['parameter2']) 

You can use javascript objects instead of an array. 您可以使用javascript对象代替数组。 Objects contain a key and a value . 对象包含一个key和一个value You can think of an object being like an array (in fact arrays are actually objects under the hood in JS), but instead of using indexes to access elements, you use keys . 您可以想到一个对象就像一个数组(实际上数组实际上是JS中的底层对象),但是您可以使用keys而不是使用索引来访问元素。

Taking your example from above you can use an object like so: 从上面举一个例子,您可以使用如下对象:

var obj = {}; // create empty object to add key-value pairs into
obj["parameter2"] = "A,B,C,D"; // add a key-value pair into the object

The above notation means, set the key to be paramter2 which has the value of "A,B,C,D" . 上述表示方法将设置为paramter2 ,其值为 "A,B,C,D" Now you can use obj["parameter2"] (or obj.parameter2 ) to access the value stored at the key paremeter2 . 现在你可以使用obj["parameter2"]obj.parameter2 )来访问存储在关键 paremeter2

See runnable example below: 请参见下面的可运行示例:

 var obj = {} obj["parameter1"] = "1,2,3,4"; obj["parameter2"] = "A,B,C,D"; console.log(obj["parameter1"]); console.log(obj["parameter2"]); console.log(obj.parameter2); // same as above (accessing value using dot notation) 

Array indices are based on a number. 数组索引基于数字。 You can add named index, but that would lead to undesired scenarios. 您可以添加命名索引,但这将导致不希望出现的情况。 You can read more about it here 您可以在这里了解更多信息

For your case, I would suggest you to use an object. 对于您的情况,我建议您使用一个对象。 example: 例:

var yourObj = {}
//If you want to push some data
if(yourObj.parameter1){
    yourObj.parameter1 = ['My Value 2']
}
else{
    yourObj.parameter1.push('My Value 2')
}

console.log(yourObj.parameter1)

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

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