如何使playerinit
从emptyseats
上卸下使用的座椅?
假设seatid
为1,那么它将从seat: 1
中删除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
};
最佳答案
使用本机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});
};