JavaScript 数组/结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4431459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Array / Struct
提问by da_didi
I would like to create a structure within javascript. I have a pair of informations, I would like to use, example:
我想在 javascript 中创建一个结构。我有一对信息,我想使用,例如:
array[1] = new Struct();
array[1].name = "parameter-name";
array[1].value = "parameter-value";
array[2] = new Struct();
array[2].name = "parameter-name2";
array[2].value = "parameter-value2";
This can be on a diffrent page with diffrent values, maybe on element within my array, maybe 2-20..
这可以在具有不同值的不同页面上,可能在我的数组中的元素上,可能是 2-20..
Later, within my generic javascript, I would like to parse the array and continue with my parameters, example:
后来,在我的通用 javascript 中,我想解析数组并继续我的参数,例如:
for(i=1 to length_of_my_array) {
_tag.array[i].name = array[i].value;
}
How can I realize this with pure javascript? Thanks for any hint!
我怎样才能用纯 javascript 实现这一点?感谢您的任何提示!
回答by casablanca
As long as you don't want any fancy features, it's really easy to create such structures in JavaScript. In fact, the code you posted will almost work, if you replace the new Struct()
with this:
只要你不想要任何花哨的功能,在 JavaScript 中创建这样的结构真的很容易。事实上,你发布的代码几乎可以工作,如果你new Struct()
用这个替换:
array[1] = {};
This creates an empty object, and you can put any properties you want in it, such as name
and value
.
这将创建一个空对象,您可以在其中放置任何您想要的属性,例如name
和value
。
To create an array, you can do something like this:
要创建数组,您可以执行以下操作:
var array = []; // empty array
// object literal notation to create your structures
array.push({ name: 'abc', value: 'def' });
array.push({ name: 'ghi', value: 'jkl' });
...
And to iterate over the array:
并遍历数组:
for (var i = 0; i < array.length; i++) {
// use array[i] here
}
回答by Adolph Trudeau
It would be good to find out more regarding the problem you are attempting to resolve.
最好了解更多有关您正在尝试解决的问题的信息。
I don't think there is an object in JavaScript called Struct, unless you define one.
我不认为 JavaScript 中有一个叫做 Struct 的对象,除非你定义一个。
I think what you are looking for is a JavaScript object instead of Struct. There are a number of ways to create a new object, and they can be nested in an array or in other objects.
我认为您正在寻找的是一个 JavaScript 对象而不是 Struct。有多种方法可以创建新对象,它们可以嵌套在数组或其他对象中。
myArray[0] = new Object();
myArray[0].name = "parameter-name";
myArray[0].value = "parameter-value";
myArray[1] = new Object();
myArray[1].name = "parameter-name2";
myArray[1].value = "parameter-value2";
Notice that I have changed your code in a couple of ways: 1. "array" is named "myArray" to clarify that we are referring to a particular array. 2. The first instance of myArray is 0. Arrays start at 0 in Javascript. 3. Struct is changed to Object.
请注意,我在几个方面更改了您的代码: 1. “array”被命名为“myArray”以阐明我们指的是一个特定的数组。2. myArray 的第一个实例是 0。在 Javascript 中数组从 0 开始。3. 结构体改为对象。
myarray = [
{
"name":"parameter-name",
"value":"parameter-value"
},
{
"name":"parameter-name2",
"value":"parameter-value2"
}
];
This is an alternative syntax for doing the same thing. It uses "literal notation" to designate an array (the square brackets), and the objects (the curly brackets).
这是做同样事情的另一种语法。它使用“文字符号”来指定数组(方括号)和对象(大括号)。
for(var i = 0; i < myArray.length; i++) {
for(key in myArray[i]) {
alert(key + " :: " myArray[i][key]);
}
}
This will loop over the array and alert you for each property of the object.
这将遍历数组并提醒您对象的每个属性。
alert(myArray[0]['value']) //parameter-value
myArray[0]['value'] = "bar";
alert(myArray[0]['value']) //bar
Each property of each object can also be assigned a new value.
每个对象的每个属性也可以分配一个新值。
回答by gnarf
You can define arrays and generic objects in pure JavaScript/json:
您可以在纯 JavaScript/json 中定义数组和通用对象:
var array = []; // empty array
array.push({name: 'parameter-name', value: 'parameter-value'});
array.push({name: 'parameter-name2', value: 'parameter-value2'});
console.log(array);
// Output:
// [Object { name="parameter-name", value="parameter-value2"}, Object { name="parameter-name2", value="parameter-value2"}]
You can also define the same array like so:
您还可以像这样定义相同的数组:
var array = [
{name: 'parameter-name', value: 'parameter-value'},
{name: 'parameter-name2', value: 'parameter-value2'}
];
As far as looping through the array:
至于遍历数组:
for (var i = 0; i<array.length; i++) {
var elem = array[i];
console.log(elem.name, elem.value);
}
// Outputs:
// parameter-name parameter-value2
// parameter-name2 parameter-value2
回答by Rob Sobers
I'd store object literals in the array, like so:
我将对象文字存储在数组中,如下所示:
var myArray = [];
myArray[0] = {name:"some name", value:"some value"};
myArray[1] = {name:"another name", value:"another value"};
for (i=0; i < myArray.length; i++) {
console.log(myArray[i].name + ' / ' + myArray[i].value);
}
回答by sebasgo
// initialize empty array
var myArray = [];
// fill it with an object - the equivalent of a struct in javascript
myArray.push({
name: 'example-name'
value: 'example-value'
});
// repeat as neccessary
// walking through the array
for (var i = 0; i < myArray.length; i++)
{
// retrieving the record
record = myArray[i];
// and accessing a field
doSomething(record.name);
}
回答by Juan Mendes
How about
怎么样
function Struct(name, value) {
this.name = name;
this.value = value;
}
arr[0] = new Struct("name1", "value1");
回答by Bobby Eickhoff
Javascript objects are loose objects: properties can be added and removed dynamically. So making a new Struct(), as you suggest, does -not- guarantee that the returned object will always have the properties you expect it to have. You have to check the availability of properties at the point of usage (duck typing):
Javascript 对象是松散对象:可以动态添加和删除属性。因此,按照您的建议,创建一个新的 Struct() 并不能保证返回的对象始终具有您期望的属性。您必须在使用时检查属性的可用性(鸭子类型):
var i, element;
for (i = 0; i < array.length; i++) {
element = array[i];
if (Object.hasOwnProperty.call(element, "name")
&& Object.hasOwnProperty.call(element, "value")) {
_tag[element.name] = element.value;
}
}
(Also, I'm just guessing that _tag is an object itself, but that wasn't clear from your example.)
(另外,我只是猜测 _tag 本身就是一个对象,但是从您的示例中并不清楚。)
You could probably use a more succinct syntax, but that depends heavily on the values of the properties. For example, you -might- be able to use something like this:
您可能可以使用更简洁的语法,但这在很大程度上取决于属性的值。例如,您 - 可能- 能够使用这样的东西:
var i, element;
for (i = 0; i < array.length; i++) {
element = array[i];
if (element.name && element.value) {
_tag[element.name] = element.value;
}
}
But you need to realize that the above condition will be false not only if one or both of the properties (name and value) are undefined but also if the value of either or both refers to the empty string, null, 0, NaN, or false.
但是您需要意识到上述条件不仅在属性(名称和值)中的一个或两个未定义时为假,而且如果其中一个或两个的值指的是空字符串、null、0、NaN 或错误的。
回答by Spudley
var array = {paramName: 'paramValue', paramName2: 'paramValue2'};
for(i in array) {
_tag.i = array.i;
}
回答by John Giotta
There is no "Struct" in JavaScript only Object
JavaScript 对象中没有“结构”
my_array = new Array();
my_array.push({name: 'john', age:31});
my_array.push({name: 'da_didi', age:120});
for (i=0; i<my_array.length; i++)
{
alert(my_array[i].name);
}