1

when I create a publication, select a category. Later in a different page, I show all the categories, but I need to show only the categories that contain publications.

in ruby ​​on rails would be something like that

- if category.posts
   #show

json of categories data

[{"Cortes":{"name":"Cortes"}, "peinados":{"name":"peinados"}}]

json of posts data

[{"category": "Cortes","title": "first"}]

in this case only the category Cortes contains a publication, I need the other does not show because there are no publications with that category. in this way I show the categories:

<ul>
   <li *ngFor="let category of categories">
      {{ category.name }}
    </li>
  </ul>

2 Answers 2

1

You can use array.filter to this as follows,

this.categories = category.posts.filter(cat=>cat.publication != null);
Sign up to request clarification or add additional context in comments.

Comments

0

solve it like this

filter (category) {
       const x = this.publicaciones.filter(post => post.category === category);
       return x.length;
    }

    getCategory(services, name) {
        const s = [];
        Object.values(services).forEach(key => {
          if(key.img) {
            if (this.filterC(key.name) >= 1) {
              s.push({ name: key.name, img: key.img });
            }
          }
        });
        this.check = s;
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.