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.

Share Improve this question edited Mar 6, 2020 at 8:48 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jan 9, 2015 at 9:36 yigamesyigames 1851 gold badge6 silver badges24 bronze badges 1
  • 1 Why not convert to the same case in the filter function? – André Laszlo Commented Jan 9, 2015 at 9:38
Add a ment  | 

2 Answers 2

Reset to default 2

Try 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