Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case insensitive array comparison in javascript

I am comparing two arrays for matched items but I need to make them case insensitive.

here is the code: credit for this code to @PatrickRoberts here

const words = ['word1', 'word2', 'word3']
const texts = [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
]

console.log(
  texts.some(
    ({ description }) => words.includes(description)
  )
)

I was able to get the second part to lower case by doing words.includes(description.toLowerCase()) but I don't know how to handle the first part: texts.some(({ description }) I should mention I have tried adding toLowerCase() to { description } like this: { description.toLowerCase() } but this does not work

any help is greatly appreciated

like image 548
Jason Avatar asked Sep 15 '25 15:09

Jason


1 Answers

Switch either to the function some or function find or function findIndex.

const words = ['Word1', 'word2', 'word3']
const texts = [{    name: 'blah',    description: 'word4'  },  {    name: 'blah2',    description: 'word1'  },  {    name: 'blah3',    description: 'word5'  }];

console.log(texts.some(({description}) => words.some((w) => w.toLowerCase() === description.toLowerCase())));
like image 94
Ele Avatar answered Sep 17 '25 05:09

Ele