0

I have array:

  list_keyword=["My","name,"JElly"];

Example 1:

  var check = list_keyword.includes('JElly');
  Result: True

Example 2:

  var param= list_keyword.includes('jelly');
  Result : False

How can I get the result True as Example 1 . Thanks

Ba Trần
  • 43
  • 3
  • 1
    `var result = list_keyword.some(x => x.toLowerCase() === searchValue.toLowerCase())` – Rajesh Nov 29 '17 at 04:52
  • [You can't with `.includes()`](https://stackoverflow.com/questions/40543888/es6-includes-but-case-insensitive) without either changing the array items to be forced into a case (*and parameter*). – Spencer Wieczorek Nov 29 '17 at 04:53
  • 2
    Possible duplicate of [Javascript contains case insensitive](https://stackoverflow.com/questions/8993773/javascript-contains-case-insensitive) – Rajesh Nov 29 '17 at 04:54
  • 1
    If you want to dive into the wonderful world of [locales](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare), `list_keyword.some(keyword => searchValue.localeCompare(keyword, 'en', {usage: 'search', sensitivity: 'accent'}) === 0)` – Ry- Nov 29 '17 at 05:57
  • Thanks you verry much – Ba Trần Nov 29 '17 at 06:46

2 Answers2

0

Simply make every string in the list lower-case, then check:

list_keyword.map(function(i){return i.toLowerCase()}).includes('jelly')
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • My bad! Understood wrong. But just a pointer, please look for dupes first – Rajesh Nov 29 '17 at 04:55
  • @ᴬᴶᵁᴾᴾᴬᴸ this is definitely a solution but do you think you 'd be better off saying `.includes('jelly'.toLowerCase())` too ? That is, wrap it in a function which takes a query string `query` and check `... .includes(query.toLowerCase())` ? – trk Nov 29 '17 at 05:09
0

You can make a function to search the string in array as below:

var arr = ["My", "name", "JELLY"];

function searchString(strValue) {
  var arrToUp = String.prototype.toUpperCase.apply(arr).split(",");
  var arrToLow = String.prototype.toLowerCase.apply(arr).split(",");
  console.log((arrToUp.includes(strValue) || arrToLow.includes(strValue) || arr.includes(strValue)) ? 'true' : 'false')
}

searchString("jelly");
searchString("JELLY");
Bhuwan
  • 16,525
  • 5
  • 34
  • 57