Does string contains array element

Authors
  • avatar
    Name
    Hamza Rahman
Published on
-
1 mins read

JavaScript .some() method can be used to easily test whether an array element is present in a String or not.

const stringTODO = 'bring some apples for guests'
const stringTODOFalsely = 'bring pizza for guests'
const basket = ['apples', 'grapes', 'watermelons', 'mangoes']
const hasFruit = basket.some(fruit => stringTODO.includes(fruit))
const hasPizza = basket.some(fruit => stringTODOFalsely.includes(fruit))
console.log('hasFruit: ', hasFruit, '\nhasPizza:', hasPizza)

OutPut:

hasFruit: true
hasPizza: false