Javascript:读取对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18895806/
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: read Array of Objects
提问by ExE
I have two .js files. In one, I want to fill an array with objects, that have two properties. In the other file I want to loop over the array and work with the objects properties.
我有两个 .js 文件。一方面,我想用具有两个属性的对象填充数组。在另一个文件中,我想遍历数组并使用对象属性。
My coding looks like this:
我的编码如下所示:
file1.js
文件1.js
var selection = new Object();
selection.column = "";
selection.term = "";
var selectionContainer = new Array();
...
press: function(){
var i;
for (i=1;i<=selectionContainer.length;i++){
selection = selectionContainer[i];
alert("Search Term: " + selection.column + selection.term);
}
}
file2.js
文件2.js
change: function(oEvent){
selection.column = "someText";
selection.term = "someOtherText";
selectionContainer[nrOfEntries] = selection;
}
When executing the javascript I receive 'Uncaught TypeError: Cannot read property 'column' of undefined'.
执行 javascript 时,我收到“未捕获的类型错误:无法读取未定义的属性‘列’”。
What am I doing wrong?
我究竟做错了什么?
采纳答案by nnnnnn
In JS arrays begin with index 0
, and the last element will be at the .length - 1
. So you need to change your loop to start at 0
and use <
rather than <=
:
在 JS 中,数组以 index 开头,0
最后一个元素位于.length - 1
. 因此,您需要更改循环以开始0
并使用<
而不是<=
:
for (i=0;i<selectionContainer.length;i++){
The error you received, Uncaught TypeError: Cannot read property 'column' of undefined'
, is because when your counter got up to selectionContainer.length
you tried to read an element just past the end of the array, which in itself doesn't give an error, it just returns undefined
, but then undefined.column
gives the error.
您收到的错误Uncaught TypeError: Cannot read property 'column' of undefined'
, 是因为当您的计数器到达时,selectionContainer.length
您试图读取刚好超过数组末尾的元素,这本身并没有给出错误,它只是返回undefined
,但随后undefined.column
给出了错误。
But also - though it's a bit hard to tell because I'm not sure about the code you don't show - in the code you doshow it looks like you are filling your array with multiple references to the sameobject, because in your change
function you update the properties of the existing selection
object and then put a reference to that object into the array. I believe you need to create a new object at that point:
而且 - 尽管有点难以判断,因为我不确定您没有显示的代码 - 在您确实显示的代码中,看起来您正在使用对同一对象的多个引用填充数组,因为在您的change
函数更新现有selection
对象的属性,然后将该对象的引用放入数组中。我相信此时您需要创建一个新对象:
change: function(oEvent){
selection = new Object();
selection.column = "someText";
selection.term = "someOtherText";
selectionContainer[nrOfEntries] = selection;
}
...but an easier way is to just use an object literal:
...但更简单的方法是使用对象字面量:
change: function(oEvent){
selectionContainer[nrOfEntries] = { column : "someText", term : "someText" };
}
You don't say what nrOfEntries
is, but you don't need a separate variable to keep track of the number of elements in the array when JS provides you with the .length
property. In any case if you're just trying to add to the end of the array you can do this:
你不说是什么nrOfEntries
,但是当JS为你提供.length
属性时,你不需要一个单独的变量来跟踪数组中元素的数量。在任何情况下,如果您只是想添加到数组的末尾,您可以这样做:
selectionContainer.push( { column : "someText", term : "someText" } );
In general it is considered best practice to use array literals and object literals to create empty arrays and objects, so the beginning of your code could be:
一般来说,使用数组字面量和对象字面量来创建空数组和对象被认为是最佳实践,因此代码的开头可能是:
var selection = {};
...
var selectionContainer = [];
回答by Krasimir
First of all - why you start the loop from the index 1. Isn't it correct if you start with the first element:
首先 - 为什么你从索引 1 开始循环。如果你从第一个元素开始是不是正确的:
for (var i=0;i<selectionContainer.length;i++) {
Second, it is better to use the push method to add elements to the array. For example:
其次,最好使用push方法向数组中添加元素。例如:
selectionContainer.push(selection);
nrOfEntries = selectionContainer.length;
If that doesn't help then we need more information about nrOfEntries and how it is changed.
如果这没有帮助,那么我们需要有关 nrOfEntries 及其更改方式的更多信息。
回答by DrakaSAN
change: function(oEvent){
selection.column = "someText";
selection.term = "someOtherText";
selectionContainer[nrOfEntries] = selection;
}
Change it to
将其更改为
change: function(oEvent) {
selectionContainer.push({column : 'someText', term : 'someOtherText});
}
You don t need i anymore, and you avoid to forget to fill selectionContainer[0]
你不再需要 i 了,你避免忘记填写 selectionContainer[0]