Reputation: 84
I have an Excel-based product list saved as .txt. I load this into JavaScript as follows:
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. Now I have a price in array[rows][14] which I want to compare. I load one line which is the existing produkt (1D Array) and my 2D array:
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 ".").
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...).
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 (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?
Upvotes: 3
Views: 125
Reputation: 84
I just had to insert an if(Eingabearray[i])
, because the error came from some empty lines...
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.
Upvotes: 1