gpt4 book ai didi

javascript - Backbone Collection .findwhere() 下划线方法

转载 作者:行者123 更新时间:2023-12-03 11:03:46 29 4
gpt4 key购买 nike

此代码返回模型在集合中出现的次数的警报消息。我想只打印一次,并在用户名和密码匹配后立即退出循环。该怎么办?

                this.collection.find(function(model)
{
debugger
var user = model.get('username');
var pwd = model.get('password');

if(enteredUsername == user && enteredPassword == pwd)
{

return(alert("success"));

}
else
{
return(alert("failure"));

}
});

最佳答案

BB的来源

    where: function(attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return this[first ? 'find' : 'filter'](function(model) {
for (var key in attrs) {
if (attrs[key] !== model.get(key)) return false;
}
return true;
});
},

Return the first model with matching attributes. Useful for simple cases of find.


findWhere: function(attrs) {
return this.where(attrs, true);
},

所以尝试一下

console.log(this.collection.findWhere({username: username, password: password}));

关于javascript - Backbone Collection .findwhere() 下划线方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27958837/

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