简体   繁体   English

删除多维数组中的空数组

[英]remove empty array in multidimensional array

here is my code in jsfiddle 这是我在jsfiddle中的代码

var app = angular.module("app", []);

app.controller("MyCtrl1", MyCtrl1);

function MyCtrl1($scope) {

    $scope.block = new Array();

    $scope.block[0] = new Array();
    $scope.block[0].push("111");
    $scope.block[0].push("112");
    $scope.block[0].push("113");
    $scope.block[0].push("114");
    $scope.block[0].push("115");

    $scope.block[2].length = 0;

    $scope.block[3] = new Array();
    $scope.block[3].push("111");
    $scope.block[3].push("112");
    $scope.block[3].push("113");
    $scope.block[3].push("114");
    $scope.block[3].push("115");


    $scope.block.filter(Boolean);

    console.log($scope.block.length.toString());

}

[["111","112","113","114","115"],["111","112","113","114","115"], [] ,["111","112","113","114","115"]] [[“ 111”,“ 112”,“ 113”,“ 114”,“ 115”],[“ 111”,“ 112”,“ 113”,“ 114”,“ 115”], [] ,[“ 111" , “112”, “113”, “114”, “115”]]

how to remove the empty array Thanks help~ 如何删除空数组谢谢帮助〜

Array#filter does not modify the array it's called on. Array#filter不会修改其调用的数组。 It returns a new array. 它返回一个新数组。

Also, Boolean([]) is true so that won't work here. 另外, Boolean([])true因此在这里不起作用。

Do this: 做这个:

$scope.block = $scope.block.filter(function (arr) {
    return arr.length;
});

http://jsfiddle.net/wmLmrqrq/ http://jsfiddle.net/wmLmrqrq/

If I understand your question correctly, this should do the trick: 如果我正确理解了您的问题,这应该可以解决问题:

$scope.block.splice(2,1)

The first parameter specifies the index of the "block" array. 第一个参数指定“块”数组的索引。 The second parameter specifies how many items to remove starting at that index. 第二个参数指定从该索引处开始删除多少个项目。

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

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