There is an array with multiple array. My aim is to build a html-table with this data. Therefore the length of each array must be the same.
Now I want to remove empty rows or colomns: If all last elements of each array are empty (=empty string) or the last array has only empty string, they should be removed.
So this
var data = [
[ 'field 1', 'field 2', '' ],
[ 'field 1', 'field 2', '' ],
[ 'field 1', 'field 2', '' ],
[ '', '', '' ]
];
should become:
var dataNew = [
[ 'field 1', 'field 2' ],
[ 'field 1', 'field 2' ],
[ 'field 1', 'field 2' ]
];
This is what I got so far:
data = removeEmpty(data);
function removeEmpty(data) {
// check for empty colomn at the end
var colHasValue = false,
len = data.length;
while (len--) {
if (data[len][data[len].length - 1]) {
unchanged = true;
break;
}
}
if (!colHasValue) {
data = data.map(function (v) {
return v.slice(0, -1);
});
}
// check for empty row at the end
var rowHasValue = false,
lastArray = data[data.length - 1],
len = lastArray.length;
while (len--) {
if (lastArray[len]) {
hasValue = true;
break;
}
}
if (!rowHasValue) {
data.pop();
}
return data;
}
This is working so far. But what should I do if there are two empty rows or colomns at the end?
Example:
var data = [
[ 'field 1', 'field 2', '', '' ],
[ 'field 1', 'field 2', '', '' ],
[ 'field 1', 'field 2', '', '' ],
[ '', '', '', '' ],
[ '', '', '', '' ]
];
should become:
var dataNew = [
[ 'field 1', 'field 2' ],
[ 'field 1', 'field 2' ],
[ 'field 1', 'field 2' ]
];
Update:
In this case nothing should be done to the array, as the last col/row aren't completely empty:
var data = [
[ 'field 1', 'field 2', '', '' ],
[ 'field 1', 'field 2', '', 'content' ],
[ 'field 1', 'field 2', 'content', '' ],
[ '', '', 'content', '' ],
[ '', 'content', '', '' ]
];