maria maria - 1 month ago 6
AngularJS Question

angularjs way to remove item from array

How can i make the playerinit remove the seat used from emptyseats?
lets say seatid is 1, then it will remove seat: 1 from emptyseats?

vm.emptyseats = [
{seat: 1},
{seat: 2},
{seat: 3},
{seat: 4},
{seat: 5},
{seat: 6},
{seat: 7},
{seat: 8},
{seat: 9},
];


vm.playerinit = function(x) {
console.log("init user data");
var seatid = x.position;

// vm.emptyseats remove where seat: seatid

};

Answer

Using native Array#filter function:

vm.playerinit = function(x) {
      console.log("init user data");
      var seatid = x.position;

      // vm.emptyseats remove where seat: seatid
      //Using ES6 arrow function syntax
      vm.emptyseats = vm.emptyseats.filter( (empty_seat) => empty_seat.seat !== seatid);
      //Or regular syntax
      vm.emptyseats = vm.emptyseats.filter( function(empty_seat) {     return empty_seat.seat !== seatid});

    };
Comments