如何让 playerinit
删除 emptyseats
中使用的座位?
假设 seatid
为 1,那么它将从 emptyseats
中删除 seat: 1
?
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
};
最佳答案
使用原生Array#filter
函数:
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});
};
关于javascript - Angularjs从数组中删除项目的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40221302/