Duke
Duke

Reputation: 175

remove empty array in multidimensional array

here is my code in 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"]]

how to remove the empty array Thanks help~

Upvotes: 0

Views: 1334

Answers (2)

Maninacan
Maninacan

Reputation: 106

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.

Upvotes: 1

JLRishe
JLRishe

Reputation: 101662

Array#filter does not modify the array it's called on. It returns a new array.

Also, Boolean([]) is true so that won't work here.

Do this:

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

http://jsfiddle.net/wmLmrqrq/

Upvotes: 3

Related Questions