[英]JavaScript 2D array iteration through one dimension doesn't work
I have an Excel -based product list saved as .txt. 我有一个基于Excel的产品列表保存为.txt。 I load this into JavaScript as follows:
我将其加载到JavaScript中,如下所示:
function make2DArray (pfad){
var Daten = loadFile(pfad);
var Array = createArray(Daten);
return Array;
}
function createArray(data_in){
var Zeilen = data_in.split("\n");
var Spalten = new Array();
for (var i=0; i<Zeilen.length; i++)
{
Spalten = Zeilen[i].split("\t");
Zeilen[i] = Spalten.splice(0, Spalten.length);
}
return Zeilen;
};
function loadFile(file_in)
{
var file_out;
$.ajax({
type: "GET",
url: file_in,
dataType: 'text',
async: false,
success: function(data){
file_out = data;
},
error: function(){
openDialog('Datei nicht gefunden', file_in + " konnte nicht gefunden werden.\n<br />Bitte wende dich an einen Troubleshooter oder Teamleader!");
}
});
return file_out;
};
That's so that you know how my Array is made... 那就是你知道我的阵列是如何制作的......
Anyway, with this I have an Array[rows][cols] to work with. 无论如何,有了这个我有一个数组[行] [cols]可以使用。 Now I have a price in array[rows][14] which I want to compare.
现在我有一个数组[行] [14]的价格,我想比较。 I load one line which is the existing produkt (1D Array) and my 2D array:
我加载一行是现有的produkt(1D数组)和我的2D数组:
function PackAusfiltern (arrzeile, arr) {
var Eingabearray = arr;
var Ausgabearray = "";
var BestandPreis = arrzeile[14];
BestandPreis = BestandPreis.replace(/\,/g, '.');
BestandPreis = parseFloat(BestandPreis);
//Here I get the price of the existing package which I want to compare
And now to my actual problem: I want to literate through every row, get the value of column 14 and convert it to a number (some numbers are stored like "39,90", so I need to change the "," to "."). 而现在我的实际问题是:我想通过每一行识别,得到第14列的值并将其转换为数字(某些数字存储为“39,90”,所以我需要将“,”更改为“ “)。
I always get an error here in this line: 我总是在这行中遇到错误:
var zwischenspeicher = Eingabearray[i][14]+"";
It says something like "for the property '14' is no value get able" (German Internet Explorer 9...). 它说“对于财产'14'这样的东西是没有价值的”(德国Internet Explorer 9 ......)。
Here is the for
loop which should get the value, make it a number, compare it an delete elements where the second line already brings the error: 这是
for
循环,它应该获取值,使其成为一个数字,将其与第二行已经带来错误的删除元素进行比较:
for (var i=0, j=Eingabearray.length; i<j; i++) {
var zwischenspeicher = Eingabearray[i][14]+"";
zwischenspeicher = zwischenspeicher.split(',').join('.');
zwischenspeicher = parseFloat(zwischenspeicher);
Eingabearray[i][14] = zwischenspeicher;
if ((BestandPreis <= Eingabearray[i][14]) && Eingabearray[i][16] == 1)
{
var löschenPO = Eingabearray.indexOf(Eingabearray[i][17]); // Find other pack code
var löschenindex = Eingabearray.indexOf(löschenPO); // Search other pack
Eingabearray = Eingabearray.splice(löschenindex, 1); // Delete other pack
}
};
Just for information: 仅供参考:
for (var i=0, j=Eingabearray.length; i<j; i++) {
alert(Eingabearray[i][14]); //= '39.90'
and 和
type(Eingabearray[i][14]); //= [object Number]
function type(obj){
alert(Object.prototype.toString.call(obj));
}
What stupid mistake have I made? 我犯了什么愚蠢的错误?
I just had to insert an if(Eingabearray[i])
, because the error came from some empty lines... 我只需插入一个
if(Eingabearray[i])
,因为错误来自一些空行......
So: 所以:
for (var i=0, j=Eingabearray.length; i<j; i++) {
if (Eingabearray[i]){
if ((BestandPreis <= Eingabearray[i][14]) && Eingabearray[i][16] == 1)
{
var löschenPO = Eingabearray.indexOf(Eingabearray[i][17]); // Find other pack code
var löschenindex = Eingabearray.indexOf(löschenPO); // Search other pack
Eingabearray = Eingabearray.splice(löschenindex, 1); // Delete other pack
};
};
};
now works fine. 现在工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.