如何使用项目 ID 而不是索引进行 Vue.set



我有实时应用程序,我在做什么,从后端获取可用数据并另存为数组。 当用户单击其将不可用的发送索引以后端将其广播到当前页面中的所有用户并隐藏不可用的数据时。

var vm = this;
this.$echo.channel("accept-coin").listen("AcceptCoin", response => {
vm.$set(vm.coins, response.indexid, response.coin);
vm.coins[response.indexid].result = response.result;
vm.$auth.fetchUser();
setTimeout(() => {
this.coins[response.indexid].hide = false;
}, 10000);
});

问题是,例如,如果当前数组有 5 个对象,而新用户打开此页面时 2 个隐藏不可用,他将只有 3 个对象

如果用户在数组中有 5 个对象,请单击第 5 个对象。 它适用于当前用户,但其他在数组中有 3 个对象的人收到错误。

我真正想要的是我想用项目 ID 而不是索引修改对象

我发现使用findIndex方法的简单解决方案

var vm = this;
this.$echo.channel("accept-coin").listen("AcceptCoin", response => {
var indexid = vm.coins.findIndex(f => f.id === response.coin.id); //<-- this find index with coin id so then i can do whatever 
vm.$set(vm.coins, indexid, response.coin);
vm.coins[indexid].result = response.result;
vm.$auth.fetchUser();
setTimeout(() => {
this.coins.splice(indexid, 1);
}, 10000);
});

希望这有帮助

最新更新