gpt4 book ai didi

javascript - 主干 - 删除集合中的最后一个模型,但只能通过过滤器

转载 作者:行者123 更新时间:2023-11-30 16:51:02 25 4
gpt4 key购买 nike

this.keysCollection.pop();

删除最后一个非常简单,但我最想做的是这个(伪解决方案):

this.keysCollection.pop({ type: 'foo' });

并从 Collection 中删除最后一个匹配的模型。可能吗?

编辑:

用这个 -

const models = this.where({ type: 'foo' });
const model = models[models.length - 1];
this.remove(model);

最佳答案

Pop 实际上只是从 backbone source 获取最后一个元素,然后对结果调用 remove

  pop: function(options) {
var model = this.at(this.length - 1);
this.remove(model, options);
return model;
},

既然是这种情况,您可以使用 where获取与您的过滤器匹配的模型,然后将结果中的最后一个模型传递给 remove自己。

var matchingModels = this.where({type: 'foo'});
this.remove(matchingModels[matchingModels.length - 1]);

关于javascript - 主干 - 删除集合中的最后一个模型,但只能通过过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30532259/

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