Use toLowerCase() on a string array - Angularjs 5, Typescript

Asked

Viewed 143 times

0

I’m trying to transform an array with strings inside in lowercase using toLowerCase, but it doesn’t seem to work with an array, but only with a string... How do I fix it?

items: Array<{tag: string[], image: string, page}>;

toggledItems() {
  this.items = [
      {
          tag: ['exemplo1','exemplo2'],
          image: '../../assets/imgs/exemplo/ccc.jpg',
          page: Ex1Page,
      },
      {
          tag: ['exemplo4', 'exemplo5', 'exemplo6'],
          image: '../../assets/imgs/exemplo/ci.jpg',
          page: Ex1Page,
      }
  ];
}

In the code below is where I add the "tag" array that is inside the "items" array. But it doesn’t work.

if (val && val.trim() != '') {
  this.items = this.items.filter((item) => {
    return (item.tag.toLowerCase().indexOf(val.toLowerCase()) > -1);
  })
}

1 answer

0


Just iterate through the object array and map each tag.

if (val && val.trim() != '') {
  for(item of items) {
    item.tag = item.tag.map(res => res.toLowerCase());
  }
}

In order to be able to filter the tags so that it is not necessary to write the whole word to find the desired array, it is interesting to use the javascript some function. Basically it will work as a find, but it just finds an element that meets the requirement to return true.

Follow the plunker: http://plnkr.co/edit/reGXZfiyBBY7GXZT81hG

  • Thank you for your reply. But when I use the code you gave me, it transforms the strings within the "tag" array into true booleans, hence the res.toLowerCase() function accuses it does not exist...

  • But what is turning the tags into boolean? Surely there is something else in your code that is doing this, I tested here with this your object and was quiet.

  • After some tests I realized that this stretch ". indexof(val.toLowerCase()) > -1" is causing the transformation into Boolean. But I don’t understand the reason... (this excerpt transforms what is written in the Archbar in Livingcase, so it is essential)

  • It is returning boolean and is normal, you are asking if the Index of the value is greater than -1 (trick to know if it exists). I think I would have no problem always making the value lowercase, even if it was not, or would have?

  • Eventually I realized the function ". indexof(val.toLowerCase()) > -1" is adding a Boolean at the end of the "res" array and acting very different from how it should...

  • No, it’s actually acting exactly the way it should. Why is that? index > -1? If yes, true, if not, false. Because you need this index?

  • It should indicate the items that will appear in the list during the search. Example: I write "example" in Archbar and it shows both items, but if I write "exemplo4" it shows only the second item. At least that’s what’s supposed to happen, but I’m having a really hard time fixing this. I’m sure there is a simple solution, but I’m beginner in programming.

  • Follow the plunker with the solution: http://plnkr.co/edit/reGXZfiyBBY7GXZT81hG

  • Thank you for the explanations and for the attention you are giving to my problem friend. I tested the code you sent me and it works just as I could make the other one work. But not the way it should. The concept is that when typing the letters, the images will appear with the corresponding letter tags until only the specific word remains. In that code ai the corresponding image only appears when a tag word is COMPLETELY written and equal to the array... For the user this is somewhat bad in the search category.

  • I just finished editing the plunker and the answer, I think this will be enough.

  • IT WORKED! Dude, you’re awesome! I know it’s something simple, but for me who am starting, making anything work is an immense joy. Make sure I remember the help and attention you gave me. Thank you!

  • Anything just call, I’m happy to help

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.