2

this functions searchs names or categories of this array of objects, i need to ignore spanish letters with 'acento' (for example: i need to match 'Taragüí' if i write taragui), i solve this with the string replace() method, my question: Is there a way to solve this with some unicode method? 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'},
    {id: 9, name: 'Taragüí', 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().replace('á', 'a').replace('é', 'e').replace('í', 'i').replace('ó', 'o').replace('ú', 'u').replace('ü', 'u').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="Enter some text" name="name"/>
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • 1
    In a nutshell: you could use `localeCompare`. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare – Pac0 Oct 15 '20 at 03:25
  • actually lthere is not `localCompare` equivalent for `includes` it seems :( – Pac0 Oct 15 '20 at 03:44
  • 1
    Does this answer your question? [Remove accents/diacritics in a string in JavaScript](https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript) – Zachary Haber Oct 15 '20 at 04:14

0 Answers0