0

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', '', '' ]
];
2
  • 1
    Why do I get a downvote for this - as I think - detailed description, examples and my own attempt? Commented Apr 2, 2016 at 20:42
  • What if the empty column/row is the first? Should it be removed? Commented Apr 2, 2016 at 21:32

3 Answers 3

1

You can do it by combining Array.prototype.map() with Array.prototype.filter()

var dataNew  = data.map(function(a){
  return a.filter(function(b){
    return b.trim().length;
  });
}).filter(function(itm){
    return itm.length;
});

Edit:

var dataNew = data.filter(function(itm){ return itm.join("").length; }), internalCnt;
for (var i = 0, len = dataNew.length; i < 4; i++) {
  for (var j = 0, cnt = 0; j < len; j++){ if(dataNew[j][i] == ""){ cnt++; } }
  if(cnt == len) {
   internalCnt = 0; 
   while(internalCnt < len){ dataNew[internalCnt].splice(i,1); internalCnt++; } i--;
  }
}

console.log(dataNew);

DEMO

Sign up to request clarification or add additional context in comments.

10 Comments

I would have achieved this in 500 lines. ;)
This won't work. Please look at the updated example. If any row or column doesn't have a 'full' dataset, the structure will be messed up.
@user3142695 What is the expected result for your updated example?
@VenomVendor I did not down vote... and I did the update transparent
@RajaprabhuAravindasamy Oh wow. Awesome. You are the greatest.
|
0

Here is one way to do it: Using map, we go through each array, then filter out the empty strings, then finally filter out the left over empty arrays.

Working Fiddle for the code below

var data = [
    [ 'field 1', 'field 2', '', '' ],
    [ 'field 1', 'field 2', '', '' ],
    [ 'field 1', 'field 2', '', '' ],
    [ '', '', '', '' ],
    [ '', '', '', '' ]
];

var newArray = data.map(function(el) {
  return el.filter(function(a) {
    return a !== '';
  });
}).filter(function(el){
  return el.length > 0;
});

console.log(newArray);

2 Comments

Unfortunatly this would mess up the structure. Example: Replace the second array with: [ 'field 1', 'field 2', '', 'content' ].
well it answered the question until you edited it just now, will delete
0

A small pivot will help with Array#reduceRight(), for going backwards and a check with Array#some() if the row is not completely empty and then add the data to the result array.

Apply twice for the right orientation.

function killEmpty(array) {
    function pivot(array) {
        var empty = false;
        return array.reduceRight(function (r, a, i) {
            empty = empty || a.some(function (b) {
                return b;
            });
            empty && a.forEach(function (b, j) {
                r[j] = r[j] || []
                r[j][i] = b;
            });
            return r;
        }, []);
    }
    return pivot(pivot(array));
}

var data = [['field 1', '', 'field 2', ''], ['field 1', '', 'field 2', ''], ['field 1', '', 'field 2', ''], ['', '', '', '']],
    data2 = [['field 1', 'field 2', '', ''], ['field 1', 'field 2', '', 'content'], ['field 1', 'field 2', 'content', ''], ['', '', 'content', ''], ['', 'content', '', '']];

document.write('<pre>pre ' + JSON.stringify(data, 0, 4) + '</pre>');
document.write('<pre>post ' + JSON.stringify(killEmpty(data), 0, 4) + '</pre>');
document.write('<hr><pre>pre ' + JSON.stringify(data2, 0, 4) + '</pre>');
document.write('<pre>post ' + JSON.stringify(killEmpty(data2), 0, 4) + '</pre>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.