gpt4 book ai didi

javascript - 按子字符串过滤结果错误

转载 作者:行者123 更新时间:2023-12-02 16:06:45 25 4
gpt4 key购买 nike

我正在创建一个过滤输入,其中根据 keyUp 带来结果。这就是我通过 Backbone 集合进行过滤的方法:

var Brand = Backbone.Model;

var Brands = Backbone.Collection.extend({
model: Brand,
filterByName: function () {
return this.filter(function (model) {
return model.get('name').indexOf('h') > -1;
});
}
});

var fiat = new Brand ({ name: 'Fiat' });
var honda = new Brand ({ name: 'Honda' });
var chevrolet = new Brand ({ name: 'Chevrolet' });
var peugeot = new Brand ({ name: 'Peugeot' });
var mitsubishi = new Brand ({ name: 'Mitsubishi' });
var hyundai = new Brand ({ name: 'Hyundai' });

var brands = new Brands ([ fiat, honda, chevrolet, peugeot, mitsubishi, hyundai ]);

console.log(brands.filterByName());

Playground: http://jsfiddle.net/Lgcb0skm/

重点是:例如,当我输入 h 时,它只会给我带来 MitsubishiChevrolet,而不是所有可能的结果,例如HondaHyundai 等等。为什么?有建议吗?

最佳答案

简短回答'H'!='h'。如果您想进行不区分大小写的匹配,则需要小写文本字符串:

return this.filter(function (model) {
return model.get('name').toLowerCase().indexOf('h') > -1;
});

关于javascript - 按子字符串过滤结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648705/

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