0

I am having below array, I am trying to find a word and it is failing as the word in the array and searching word has case differences. How can we solve this?

group = ['KIRAN','RAJESH','suresh','aditya'];
name = 'rajesh';
group.find(x => x === name)

console output: undefined

name = 'RAJESH';
group.find(x => x === name)

console output : RAJESH

name = 'SURESH';
group.find(x => x === name)

console output : undefined

any idea?

  • 2
    `group.find(x => x.toUpperCase() === name.toUpperCase())` – Ivar Jul 21 '20 at 14:03
  • 2
    @Ivar - although in the whole scheme of things it makes little difference to performance, I would do the upper-case on the name variable outside of the `find` so it doesn't happen on each individual comparison – freefaller Jul 21 '20 at 14:05
  • thanks Ivar, that is the solution but we have to convert every word into CAPS. – user2287528 Jul 21 '20 at 17:06
  • @user2287528 No, that is the solution that *makes* all words in the array in caps, and works on arbitrary arrays. – Bergi Jul 21 '20 at 17:07

0 Answers0