Home > Mobile >  Narrow search between two inputs
Narrow search between two inputs

Time:10-19

I have two inputs fields, one text, the other select, I want to narrow the search based on each input. If both input fields have data they narrow down the search result. Otherwise they search the original array.

let dataArray = [{name: 'DataOne', place: 'italy'}, {name: 'Second data', place: 'berlin'}, {name: 'third data', place: 'barcelona'} ]

let searchStringName = ''
let searchSelectCity = ''

CodePudding user response:

Are you expecting something like this?

var dataArray = [{name: 'DataOne', place: 'italy'}, {name: 'Second data', place: 'berlin'}, {name: 'third data', place: 'barcelona'} ]

var name = 'data'
var city = 'berlin'
function search(){
if(name&&city){
 return dataArray.filter((a)=>(a.name).includes(name)&&(a.place).includes(city))
}
else if(name){
  return dataArray.filter((a)=>(a.name).includes(name))
}
else if(city){
  return dataArray.filter((a)=>(a.place).includes(city))
}
else 
return dataArray
}
console.log(search())

  • Related