Javascript Array filter() search array

Introduction

Following example uses filter() to filter array content based on search criteria.

let languages = ['HTML', 'CSS', 'C++', 'Java', 'Javascript']

//Filter array items based on search criteria (query)
function filterItems(arr, string) {
  return arr.filter(function(el) {
      return el.toLowerCase().indexOf(string) !== -1
  })//  www.  ja v a2  s  .co  m
}

console.log(filterItems(languages, 'C')); 
console.log(filterItems(languages, 'J'));



PreviousNext

Related