How can I filter these items?

0

Good afternoon, I have the following problem and it is that I am trying to filter some items and I am not achieving it.

The function of this would be the following:

I have a header with checkboxes that are the categories, but, what I need is that not all the categories are shown, only those that are being used.

Then I have here the object that brings all the categories of the API

What I need would be applying a filter() filter these categories so that only those that are also in this array are shown that is what brings the categories used.

["3D", "VR", "Audio-Visual", "WebGL", "Sound", "Physics", "AI", "2D"]

Try something like this based on another filter that I had done for something else on the site but it did not work for me.

getTags() {
		fetch(backend()+'/api/tags', {
      method: 'GET'
    }).then(result => result.json())
			.then(tags => {this.setState({tags});
          var arr = [];
          var backup = this.state.categories;

      for (var i = 0; i < tags.length; i++) {
            arr.push(this.state.tags[i].name)
        }

            tags = this.uniqueArray(arr);

            const categories = backup.filter(cat => {
            const _tags = tags
            const hasTags = _tags.filter(tag => tags.includes(tag));
            if (hasTags.length) {
              return cat;
            }
          });
          this.setState({categories});

			});
	}

Any ideas?

    
asked by Santiago D'Antuoni 21.04.2017 в 21:33
source

1 answer

1

Knowing that:

  • You have an array of objects tags, which represent all the tags.
  • You have an array with the names of the tags used.

Just do a basic filtering:

allTags.filter(tag => usedTags.includes(tag.name));
    
answered by 21.04.2017 / 21:56
source