0

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

2
  • Yeah, the reason that's returning the whole vehicles array is that it's matching the keys as well Commented Dec 29, 2020 at 2:38
  • Do you only want it to search the Brand? Because in your example a search for "y" should return the other Motorc"y"cle as well. Commented Dec 29, 2020 at 2:40

3 Answers 3

1

You could search for values which, when turned into strings, contain the substring being searched for:

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
}];

const input = 'y'.toLowerCase();
console.log(
  vehicles.filter(
    vehicle => Object.values(vehicle).some(
      val => String(val).toLowerCase().includes(input)
    )
  )
);

If you wanted to be fancier, you could sort the results by which has the most occurrences of the substring, or give extra weight to values that start with the input, or sort by the levenshtein distance from the input.

Sign up to request clarification or add additional context in comments.

1 Comment

That function would never find any entries containing a capital letter as input.
1

The following implementation uses your JSON.stringify logic but only applies it to the values in each object.

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,
  },
];

function filterByValue(array, value) {
  return array.filter(
    (data) =>
      JSON.stringify(Object.values(data))
        .toLowerCase()
        .indexOf(value.toLowerCase()) !== -1
  );
}

console.log(filterByValue(vehicles, "y"));

Comments

1

This would check for value only in the property values of the objects in your 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
  }
]


const filterByValue = (array, value) => {
  return array.filter((data) =>
    Object.values(data).some((val) =>
      val.toString().toLowerCase().includes(value.toLowerCase()),
    )
  )
}

console.log(filterByValue(vehicles, 'y'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.