0

To get objects in this aray, this function filters names and categories from an array of objects, the issue here is when i type for example: "jamm" (more than one 'm'), i get an empty array, even if the input words are corrects. My goal is to match right words no matter how much. I think a regular expresion could solve this. Thanks in advance

var fromDB = [
    {id: 1, name: 'Almendras', category: 'Frutos secos', price: 25, amount: 0, description: 'asd'},
    {id: 2, name: 'Nueces', category: 'Frutos secos', price: 10, amount: 0, description: 'asd'},
    {id: 3, name: 'Mermelada', category: 'Jam', price: 15, amount: 0, description: 'asd'},
    {id: 4, name: 'Alfajor', category: 'Sweet', price: 20, amount: 0, description: 'asd'},
    {id: 5, name: 'Queso', category: 'UwU', price: 45, amount: 0, description: 'asd'},
    {id: 6, name: 'Arandanos', category: 'Fruta', price: 50, amount: 0, description: 'asd'},
    {id: 7, name: 'Maracuya', category: 'Fruta', price: 50, amount: 0, description: 'asd'},
    {id: 8, name: 'Chocolate', category: 'Sweet', price: 50, amount: 0, description: 'asd'},
    {id: 9, name: 'Mascarpone', category: 'UwU', price: 50, amount: 0, description: 'asd'}
];

const input = document.querySelector('input');
input.addEventListener('input', updateValue);

function updateValue(e) {
  realTimeInputValue = e.target.value.toLowerCase();
  let productsMatch = fromDB.filter(x => x.name.toLowerCase().includes(realTimeInputValue))

  if(productsMatch.length){
    console.log(productsMatch)
  } else {
    let categoriesMatch = fromDB.filter(x => x.category.toLowerCase().includes(realTimeInputValue))
    console.log(categoriesMatch);
  }
}
<input type="text" placeholder="Search product..."/>
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • 2
    What should "jamm" match? – VLAZ Sep 17 '20 at 05:03
  • Should match products with category: 'Jam' – sonEtLumiere Sep 17 '20 at 05:03
  • "jamm" doesn't match any of your item names or categories. Shouldn't the array be empty in that case? – Cully Sep 17 '20 at 05:17
  • 1
    If you mean that you want to match, even if the search query is "misspelled", then you might want to look into something like a Levenshtein distance algorithm. – Cully Sep 17 '20 at 05:20
  • Does this answer your question? [Javascript fuzzy search that makes sense](https://stackoverflow.com/questions/23305000/javascript-fuzzy-search-that-makes-sense) – Cully Sep 17 '20 at 05:23
  • Thanks for your answer. That's a possible way to solve this, but i'm looking to improve this code snipet. – sonEtLumiere Sep 17 '20 at 05:28
  • So are you wanting to match results even if the query is "misspelled"? Is that the question you're asking? – Cully Sep 17 '20 at 05:31
  • misspelled with right words – sonEtLumiere Sep 17 '20 at 05:33
  • 2
    If you want to match something that is "close" to what you have in your database, but not an exact match, you'll have to implement a fuzzy search algorithm of some kind. Or use a library that implements it. Or use something native in your database to match (e.g. mysql has something called "full text search"). – Cully Sep 17 '20 at 05:37
  • In this case i need to match over an array of objects, i'm reading about fuzzy search algorithms, thanks for your answer. – sonEtLumiere Sep 17 '20 at 05:41

0 Answers0