用字符串和数字过滤数组的最简洁方法

时间:2020-12-29 02:36:40

标签: javascript

我想知道哪种最简洁的方法可以过滤混合字符串和数字的数组,以根据函数的参数给出最佳结果。

我有这个数组

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

例如,当我搜索 'y' 时,我想要函数返回

{
  Brand: 'Yamaha',
  Type: 'Motorcycle',
  Modelo: 'YBR',
  Cilindrada: '160cc',
  Precio: 80500
}

我的第一个方法是这个函数

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

但它返回整个 vehicles 数组

3 个答案:

答案 0 :(得分:1)

您可以搜索在转换为字符串时包含正在搜索的子字符串的值:

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)
    )
  )
);

如果您想更漂亮一些,您可以根据子字符串出现次数最多的结果对结果进行排序,或者为以输入开头的值赋予额外的权重,或者根据与输入的 levenshtein 距离进行排序。

答案 1 :(得分:1)

以下实现使用您的 JSON.stringify 逻辑,但仅将其应用于每个对象中的 values

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"));

答案 2 :(得分:1)

这只会在数组中对象的属性值中检查 value

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'));