按子字符串过滤的结果错误

Wrong result for filtering by substring

本文关键字:结果 错误 过滤 字符串      更新时间:2023-09-26

我正在创建一个过滤输入,根据keyUp带来结果。这就是我正在做的过滤,通过主干的集合:

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());

游乐场:http://jsfiddle.net/Lgcb0skm/

关键是:当我输入h时,例如,它只给我带来三菱h I Chevrolet,而不是所有可能的结果,例如 h onda h云代等。为什么?建议吗?

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

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

试试:

return model.get('name').toLowerCase().indexOf('h') > -1;