JavaScript Filter and Match

Filter an Array and find a matching element

Published 2021-07-16

JavaScript Filter & Match

If you have an Array of Strings, you can filter an Array and find a matching String with a Function :

const cities = ['New York', 'Los Angeles', 'San Francisco', 'Austin']

function findCity(array, word) {
  return array.filter((element) => {
    return element.match(word)
  })
}

console.log(findCity(cities, 'York'))
// [ 'New York' ]

Related Tutorials :