Search on mobile using JS transformers seems to not work with my array

HI,

I have an array of object stored in a variable called Bibliografia
the first two item are here below

[
  {
    "editore": "Fabbri Editore",
    "raccolta": "f",
    "confermato": "si",
    "ed_2Anno": 0,
    "**titolo**": "Il morto di Maigret",
    "ed_1Posseduta": "f",
    "lingua": "Italiano",
    "posseduto": "no",
    "ed_1Anno": 0,
    "numeroCollana": 29,
    "annoPubblicazione": 2003,
    "edizione": "Prima",
    "id": 678,
    "uniqueId": "005aa2cc-9f5f-489e-809f-886059c04e84",
    "collana": "Le grandi incheste del commissario",
    "edizioni": [
      {
        "anno": "1950",
        "edizione": "prima",
        "posseduto": false
      }
    ],
    "titoloOriginale": "",
    "_id": "005aa2cc-9f5f-489e-809f-886059c04e84",
    "__metadata": {
      "segments": [
        "Bibliografia",
        "005aa2cc-9f5f-489e-809f-886059c04e84"
      ]
    },
    "signedUrl": null,
    "bookId": 3103
  },
  {
    "editore": "Adelphi",
    "raccolta": "f",
    "confermato": "si",
    "ed_2Anno": 0,
    "titolo": "Maigret e i vecchi signori",
    "ed_1Posseduta": "f",
    "lingua": "Italiano",
    "annoScrittura": 1960,
    "posseduto": "Sì",
    "ed_1Anno": 0,
    "numeroCollana": 331,
    "bookId": 1001,
    "traduttore": "Barbara Bertoni",
    "copertina": "cover__id1913 .jpg",
    "annoPubblicazione": 2008,
    "edizione": "Prima",
    "id": 515,
    "uniqueId": "00621dc8-d725-4033-9be7-9c2560089a11",
    "collana": "gli Adelphi - Le inchieste di Maigret",
    "_id": "00621dc8-d725-4033-9be7-9c2560089a11",
    "__metadata": {
      "segments": [
        "Bibliografia",
        "00621dc8-d725-4033-9be7-9c2560089a11"
      ]
    },
    "signedUrl": null
  }
]

I implemented the search using the field "titolo" as the search field

const books = {{Bibliografia.value}};

const result = books.filter(book => book.titolo.toLowerCase().includes({{textInput1.value}}.toLowerCase()));

return result

but the result is always empty

adding a console.log(books) the result is the following

Screenshot 2024-02-13 184805

Any suggestion?

Thank you
Giovanni

Hi @Prinapo

Your transformer/filter code looks good. However, I think what is happening is the toLowerCase() function doesn't know how to handle special characters. Since the first object has titolo wrapped in ** characters:
image
toLowerCase() errors out. If I remove the ** on either side, it seems to work properly.

Also If I change your filter code to use the "editore" field, it works properly.

const books = {{Bibliografia.value}};

const result = books.filter(book => book.editore.toLowerCase().includes({{textInput1.value}}.toLowerCase()));

return result

Two approaches that may work would be to "clean" your variable of any special characters in the titolo field, or in your filter code remove any special characters before trying to filter.

Hope this helps!

Thank you! this i probably the problem, I'll correct it, to avoid this error I add a check to see if the field exist

const books = {{Bibliografia.value}} || []; // Add a null check to ensure 'books' is defined
// Get the value from textInput1 and convert it to lowercase
const searchText = {{textInput1.value}}?.toLowerCase() || '';
// Filter the books array based on whether the title (titolo) includes the search text
const result = books.filter(book => {
    // Ensure 'book' and 'titolo' are defined before calling toLowerCase()
    return book && book.titolo && book.titolo.toLowerCase().includes(searchText);
});

return result;
2 Likes