字符串indexOf总是返回-1



我在尝试使用array过滤数组时卡住了。过滤方法。

请看看我的代码:

$scope.products = CachedData.Products.filter(function (item) {
    var itemNameSlug = Helpers.slug(item.Name); // eg: product100
    var keywordSlug = Helpers.slug($scope.keyword); // eg: p, pr, pro, duct etc...
    var result = itemNameSlug.indexOf(keywordSlug);
    console.log(result); // always -1
    return  result > -1;
});

当我键入keyword作为alphabet时,它总是返回空数组(即indexOf返回-1)。如果我输入数字,例如1或10或100或00,我得到了正确的结果。

为什么?

任何帮助将不胜感激。由于

@ trevh根据你评论中的输出,你在中间有一个尾随空间当在干草堆中搜索指针时,如果指针得到一个尾随空格,则不可能匹配(因为我们在这里处理的是不包含空格的鼻涕虫)

/* according to your comment: */
console.log('-' + itemNameSlug + '-', '-' + keywordSlug + '-', typeof itemNameSlug, typeof keywordSlug);
/* gives: -product95 - -pro - string string */

那么我就会说针头是"pro ",这与"product95 "不匹配

最新更新