admin管理员组文章数量:1405607
I have a webpage where if something typed in search box it then iterates through a file for that value. I need it so that search/find not case sensitive. The case sensitivity must remain in the file but for parison purposes it disregards the case.
So currently I am using underscore mand:
arr=_.where(arr,filter);
but the 2 arrays arr
and filter
- I need them to be pared/used regardless of case so the end result arr contains results which will mixture of upper and lower case but matches value(s) in arr
.
I have a webpage where if something typed in search box it then iterates through a file for that value. I need it so that search/find not case sensitive. The case sensitivity must remain in the file but for parison purposes it disregards the case.
So currently I am using underscore mand:
arr=_.where(arr,filter);
but the 2 arrays arr
and filter
- I need them to be pared/used regardless of case so the end result arr contains results which will mixture of upper and lower case but matches value(s) in arr
.
- 1 Why not convert to the same case in the filter function? – André Laszlo Commented Jan 9, 2015 at 9:38
2 Answers
Reset to default 2Try using filter
instead:
var filter = ["Apple", "bANAna", "orange"];
var arr = ["apPle", "ORANGE"];
// make filter lower case once
var filterLower = _.invoke(filter, "toLowerCase");
var arr2 = _.filter(arr, function(v) {
// make entry lower case and see if it is in filterLower
return _.contains(filterLower, v.toLowerCase());
});
console.dir(arr2);
<script src="http://cdnjs.cloudflare./ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
Unfortunately, JS is not great when it es to internationalized case-insensitive string parison. If you're just sticking to ASCII though, the solution is fairly straightforward (using filter
though, not where
):
function getMatches(query, arr) {
var lowerQuery = query.toLowerCase();
return _.filter(arr, function(term) {
return term.toLowerCase() == lowerQuery;
});
}
Or if you want to prepute everything because you expect to make a lot of queries in the same JS session:
var index = _.groupBy(arr, function(term) { return term.toLowerCase(); });
// Post-process index to reduce memory footprint by turning redundant values to nulls
// (which take up less memory than arrays with a string in them).
index = _.object(_.map(index, function(terms, key) {
return [key, (terms.length == 1 && terms[0].toLowerCase() == terms[0] ? null : terms)];
}));
function getMatches(query) {
var lowerQuery = query.toLowerCase();
return (lowerQuery in index ? index[lowerQuery] || lowerQuery : []);
}
This second method has the advantage of only puting toLowercase()
the minimal number of times and minimal data storage because of the post-processing step.
Here's a JSFiddle for both
本文标签: javascriptHow to use where() in underscore to compare values regardless of caseStack Overflow
版权声明:本文标题:javascript - How to use _.where() in underscore to compare values regardless of case - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744329297a2600884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论