删除具有特定匹配的数组

时间:2016-03-24 18:42:50

标签: javascript angularjs ionic-framework

我正在尝试删除name="All"$scope.selectedAccounts所在的数组,但是我现在正在做的事情,$scope.selectedAccounts返回空。

我做错了什么?

controller.js

function AccountController($scope, DummyAccountService) {
    // this works, but returns everything... I need it for something else
    $scope.accounts = DummyAccountService.dummyAccounts; 

    // this doesn't return anything
    $scope.selectedAccounts = function() {
        var selectedAccountsArray = [];
        for(i=0; i < $scope.accounts.length; i++) {
            var account = $scope.accounts[i];
            if($scope.accounts.name != "All") {
                selectedAccountsArray.push(account);
            }
        }
        return selectedAccountsArray;
    }
}

services.js

function DummyAccount(id, profileId, name, tag, icon, positive, negative, neutral, currentRating) {
    this.id = id;
    this.profileId = profileId;
    this.name = name;
    this.tag = tag;
    this.icon = icon;
    this.positive = positive;
    this.negative = negative;
    this.neutral = neutral;
    this.currentRating = currentRating;
}

function DummyAccountService() {
    this.dummyAccounts = [
        new DummyAccount(1, 1, 'Facebook', 'Remy Danton', 'ion-social-facebook', 61, 12, 27, "C"),
        new DummyAccount(2, 1, 'Twitter', '@remydanton', 'ion-social-twitter', 75, 12, 13, "B"),
        new DummyAccount(3, 1, 'Web Agent', 'Remy Danton', 'ion-earth', 87, 10, 3, "A"),
        new DummyAccount(4, 1, 'All', 'Remy Danton', 'ion-person', 64, 11, 25, "B")
    ]
}

更新:我已在下方进行了所有建议的更改,但selectedAccounts仍然为空。不确定发生了什么......我已在此处粘贴了我的全部代码:http://pastebin.com/JtM8nq3X

enter image description here

4 个答案:

答案 0 :(得分:1)

if($scope.accounts.name != "All") {更改为if(account.name != "All") {$scope.accounts没有name字段,因此如果永远不会推送account

答案 1 :(得分:1)

变化

if($scope.accounts.name != "All") {
      selectedAccountsArray.push(account);
}

if($scope.accounts[i].name !== "All") {
      selectedAccountsArray.push(account);
}

- 我认为您希望$ scope.selectedAccounts成为新数组,但是您已将其设置为函数 - 您必须执行该函数,以便使用您想要的结果填充$ scope.selectedAccounts

$scope.selectedAccounts = (function() {
    var selectedAccountsArray = [];
    for(i=0; i < $scope.accounts.length; i++) {
        var account = $scope.accounts[i];
        if($scope.accounts.name != "All") {
            selectedAccountsArray.push(account);
        }
    }
    return selectedAccountsArray;
})();

将其包装在IIFE中

答案 2 :(得分:1)

其他人已经指出了语法错误;除了nameAll $scope.selectedAccounts = function () { return $scope.accounts.filter(function (acc) { return acc.name != "All"; }); } 的帐户外,FWIW还提供了一种更清晰的方式来退货:

.vertical-align {
  height: 150px; /* Height is important or it won't work */
  display: table-cell; /* We will give our div table cell property */
  vertical-align: middle; /* This will do the trick to center vertically inner element */
}

答案 3 :(得分:1)

您缺少数组的索引值或未比较本地帐户变量。在这里创建了Plunkar https://plnkr.co/edit/eByE5q4qApjjf8rUtEPz?p=preview

更改此

for(i=0; i < $scope.accounts.length; i++) {
        var account = $scope.accounts[i];
        if($scope.accounts.name != "All") {
            selectedAccountsArray.push(account);
        }
    }

for(i=0; i < $scope.accounts.length; i++) {
        var account = $scope.accounts[i];
        if(account.name != "All") {
            selectedAccountsArray.push(account);
        }
    }