简体   繁体   中英

Remove each last element of nested array and complete last array

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', '', '' ]
];

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

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);

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>'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM