I wonder which is the cleanest way to filter an array that have a mix of strings and numbers to give the best result based in the parameter of the function.
I have this array
const vehicles = [{
Brand: 'Peugeot',
Type: 'Car',
Modelo: '206',
Puertas: 4,
Precio: 200000
},
{
Brand: 'Honda',
Type: 'Motorcycle',
Modelo: 'Titan',
Cilindrada: '125cc',
Precio: 60000
},{
Brand: 'Peugeot',
Type: 'Car',
Modelo: '208',
Puertas: 5,
Precio: 250000
},{
Brand: 'Yamaha',
Type: 'Motorcycle',
Modelo: 'YBR',
Cilindrada: '160cc',
Precio: 80500
}]
And I want for example when I search for 'y' the function return
{
Brand: 'Yamaha',
Type: 'Motorcycle',
Modelo: 'YBR',
Cilindrada: '160cc',
Precio: 80500
}
My first approach was this function
function filterByValue(array, value) {
return array.filter((data) => JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1);
}
But it returns the whole vehicles array