gpt4 book ai didi

javascript - Angularjs 从数组中删除项目的方法

转载 作者:行者123 更新时间:2023-11-29 21:14:10 26 4
gpt4 key购买 nike

如何让 playerinitemptyseats 中删除使用的座位?假设 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com