Angular - Filter to remove blank strings from array

后端 未结 4 526
甜味超标
甜味超标 2021-01-16 01:46

I have an object of arrays... The array can contain blanks, how can i create an ANgular filter to remove the blanks to determine the length of the array?

$sc         


        
4条回答
  •  庸人自扰
    2021-01-16 02:21

    At first I want to note, that your JS object is invalid.

    Please, use the array of arrays instead of object of arrays in this case.

    $scope.myData = [
       ["1", "2", "3", "4", "5", "6", "7", "8", "9", "", "", "", "", "", "", "", "", "" ],
       ["1", "2", "3", "4", "5", "6", "7", "8", "", "", "", "", "", "", "", "", "", "" ],
       ["1", "2", "3", "4", "5", "6", "7", "", "", "", "", "", "", "", "", "", "", "" ],
       ["1", "2", "3", "4", "5", "6", "", "", "", "", "", "", "", "", "", "", "", "" ]
    ];
    

    And secondly i've created filter to remove the blank items.

    .filter('removeBlankItems', function() {
        return function(inputArray) {
          var outArray = [];
          for (var i = 0; i < inputArray.length; i++) {
              if(inputArray[i].length != 0){
                 outArray.push(inputArray[i]);     
              }
          }
          return outArray;
        };
    })
    

    This is JSFiddle with working example.

提交回复
热议问题