我正在编写一个for循环,它迭代一个数组,该数组在文本文档中包含不同的行。
每次迭代,我都试图提取每个文本数据行的某些部分,并将它们添加到一个对象,即LocaliNodeData。
在每次迭代结束时,我都尝试将localinoData对象(每个对象属性都设置为一个新属性)添加到一个新数组中。
在将每行中找到的字符串添加到数据数组(localinoDataObjArray)时,我会为每个数组获得不同的值。然而,当我现在在每次迭代中更改LocaliNodeData对象属性并尝试将更改后的对象附加到数组中时,我在所有数组位置都会得到相同的对象,例如
警报(LocalNodeDataObjarray[x].x\u COORD);
对于x的所有值,x\u坐标都是相同的。
function GetlocalinoDataFromFile(localinoDataFile){
var localinoDataObjArray = new Array();
var localinoData = {
time: null,
tagID: null,
X_COORD: null,
Y_COORD: null,
Z_COORD: null,
};
var allData = localinoDataFile.responseText;
var arrayOfDataLines = allData.split('\n');
// for each iteration, get the tagID, and the co-ords(seperate) and then
// create a localinoData object and add to array
for (var i = 0; i < arrayOfDataLines.length; i++) {
if (/tag: [0-9]{22}/.test(arrayOfDataLines[i])) {
var tagID = /tag: [0-9]{22}/.exec(arrayOfDataLines[i]);
localinoData.tagID = tagID;
}
if (/[0-9]+.[0-9]{3}, [0-9]+.[0-9]{3}, [0-9].[0-9]{3}/.test(arrayOfDataLines[i])) {
XYZ_COORDS = /[0-9]+.[0-9]{3}, [0-9]+.[0-9]{3}, [0-9].[0-9]{3}/.exec(arrayOfDataLines[i]);
temp = XYZ_COORDS.toString();
temp2 = temp.split(', ');
// Here I am changing the object to new values each loop
localinoData.X_COORD = temp2[0];
localinoData.Y_COORD = temp2[1];
localinoData.Z_COORD = temp2[2];
}
// Here I am appending the newly changed object to my array
// however the same object is being added every time
// (which corresponds to the last line in the text document)
localinoDataObjArray.push(localinoData);
}
// the object values for localinoDataObjArray[0]
// and localinoDataObjArray[50] are the exact same
alert(localinoDataObjArray[20].X_COORD);
}
我希望所有数组值都与文本文档中的不同行相对应。但是,所有数组值都是相同的(等于文本文档中最后一行迭代的预期结果)。
我很困惑,因为当I=0时,它应该添加一个对象,该对象具有文本文档中第一行的值,但是它显示了我期望从文档中最后一行得到的值。
这对我来说很奇怪,似乎是一个循环问题?我很困惑,希望能在这件事上得到任何帮助。
它们都是对您不断覆盖的同一对象的引用。尝试在for循环的每次迭代中创建一个新的localinoData实例。
function GetlocalinoDataFromFile(localinoDataFile){
var localinoDataObjArray = new Array();
var allData = localinoDataFile.responseText;
var arrayOfDataLines = allData.split('\n');
// alert(arrayOfDataLines[4]);
// for each iteration, get the tagID, and the co-ords(seperate) and then create a localinoData object and add to array
for (var i = 0; i < arrayOfDataLines.length; i++) {
var localinoData = {
time: null,
tagID: null,
X_COORD: null,
Y_COORD: null,
Z_COORD: null,
};
if (/tag: [0-9]{22}/.test(arrayOfDataLines[i])) {
var tagID = /tag: [0-9]{22}/.exec(arrayOfDataLines[i]);
localinoData.tagID = tagID;
}
if (/[0-9]+.[0-9]{3}, [0-9]+.[0-9]{3}, [0-9].[0-9]{3}/.test(arrayOfDataLines[i])) {
XYZ_COORDS = /[0-9]+.[0-9]{3}, [0-9]+.[0-9]{3}, [0-9].[0-9]{3}/.exec(arrayOfDataLines[i]);
temp = XYZ_COORDS.toString();
temp2 = temp.split(', ');
localinoData.X_COORD = temp2[0];
localinoData.Y_COORD = temp2[1];
localinoData.Z_COORD = temp2[2];
}
localinoDataObjArray.push(localinoData);
}
alert(localinoDataObjArray[20].X_COORD);
}
问题内容: 什么是执行以下操作的最佳方法: 但是,事情是我不想创建具有相同名称的对象,我想创建具有不同名称的对象,例如,我想将每个对象添加到列表中。做这个的最好方式是什么 ? 问题答案: 您无需为每个对象使用不同的名称。由于o1对象是在for循环中声明的,因此o1变量的范围仅限于for循环,并且在每次迭代过程中都会重新创建它…除非每次它将引用该迭代过程中创建的新对象。请注意,变量本身不存储在列表中
我一直在使用JavaScript代码进行自动GUI测试。我一直在进行全面的测试,并优化了一些代码。例如,我有一个函数可以将值输入到窗口的所有字段中,该窗口大致如下所示: 最初输入函数看起来是这样的:这是相当笨拙的,占用了很多空间,不如编辑函数以接受一个将字段作为属性值的对象那样整洁。现在函数看起来像这样:其中是这样定义的: 在我进入测试一个特殊需求的部分之前,这一直工作得很好,该需求说明当您在窗口
问题内容: 我尝试过一些关于绑定和未绑定方法的代码。当我们调用它们时,我认为它们都会返回对象。但是,当我用于获取一些信息时,它返回的内容我并不理解。 IDE:Eclipse 插件:pydev 输出是… 为什么#1和#2返回相同的ID?他们不是不同的对象吗?如果我们分配和两个变量,#3,#4回报不同的ID。 我认为#3和#4表明它们不是同一对象,而是#1和#2 … 绑定方法的ID和未绑定方法的ID有
我对编程有点陌生,我被卡住了。假设我在一个项目中有五个不同的类:foo1、foo2、foo3、foo4和foo5,它们都做不同但相似的事情。现在假设我需要为每个对象创建一个新对象,比如:foo1 bar1=new foo1();foo2 bar2=新的foo2();foo3 bar3=新的foo3();等等当然这是可行的,但如果我能在一个for循环中实例化我需要的所有对象,或者至少把我想要创建的所
下面是一个实现: 下面是一个示例用法来检查可操作性:
在阅读equals()和hashcode()时,我了解到,如果两个对象相等,那么它们的hashcode必须相等,反之亦然。 但是下面的例子并没有反映这一点。 如果是,则返回true,即使它们的哈希代码不同,这从打印和中可以明显看出。 有人能给我解释一下吗?