Jason
Jason

Reputation: 1141

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

Upvotes: 4

Views: 1897

Answers (4)

sachinkondana
sachinkondana

Reputation: 671

Probably localeCompare js API are the best and is supported by most of the browsers!

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.localeCompare(description, undefined, { sensitivity: 'base' }) === 0)));

Upvotes: 0

molamk
molamk

Reputation: 4116

  1. Use JSON.stringify to convert your objects into strings
  2. Apply .toLowerCase to the obtained strings so that everything (including all values) becomes lowercase
  3. Convert back into an object or array using JSON.parse
  4. Apply the rest of the matching logic with Array.some and Array.includes

const words = ['WORD1', 'WORD2', 'WORD3'];
const texts = [
    {name: 'blah', description: 'word4'},
    {name: 'blah2', description: 'word1'},
    {name: 'blah3', description: 'word5'}
];

const lower = x => JSON.parse(JSON.stringify(x).toLowerCase());
const [lowerWords, lowerTexts] = [words, texts].map(lower);

console.log(
  lowerTexts.some(
    ({ description }) => lowerWords.includes(description)
  )
)

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

No, it's not possible to change it during the destructuring process - this answer explains why.

It's much easier to check by using some instead of includes:

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(word => word.toLowerCase == description.toLowerCase())
  )
)

Upvotes: 2

Ele
Ele

Reputation: 33726

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())));

Upvotes: 3

Related Questions