简体   繁体   English

删除嵌套数组的每个最后一个元素并完成最后一个数组

[英]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. 我的目标是用此数据构建一个html表。 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: 在这种情况下,不应对数组进行任何操作,因为最后一个col / row并非完全为空:

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() 您可以通过将Array.prototype.map()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. 这是一种实现方法:使用map,我们遍历每个数组,然后过滤掉空字符串,最后过滤掉剩下的空数组。

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. 一个小的枢轴将有助于Array#reduceRight()进行后退,并使用Array#some()检查该行是否不完全为空,然后将数据添加到结果数组中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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