Use toLowerCase () in an array of strings - AngularJS 5, TypeScript

0

I am trying to transform an array with strings inside in lowercase using toLowerCase, but it seems that it does not work with an array and only with a string ... How do I solve this?

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 array "tag" that is inside the "items" array. But it does not work.

if (val && val.trim() != '') {
  this.items = this.items.filter((item) => {
    return (item.tag.toLowerCase().indexOf(val.toLowerCase()) > -1);
  })
}
    
asked by anonymous 25.11.2017 / 04:32

1 answer

0

All you have to do is iterate through the array of objects and map each tag.

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

In order to filter the tags so that it is not necessary to type the entire word to find the desired array, it is interesting to use the javascript's add function. Basically it will work as a find, but just find an element that meets the requirement to return true.

Follow the plunker: link

    
25.11.2017 / 16:59