0

Building a project in VueJS with data coming from an api (using axios): I have a list of projects with props (year, location, etc) and tags (house, park, etc). I have made this filter to toggle the sort by prop:

sortby(data) {
 // data = {prop: "year", tag: "house"}
 //
 if (data.prop === "year") {
   this.projects.sort((a, b) => (a[data.prop] < b[data.prop] ? 1 : -1));
 } else {
   this.projects.sort((a, b) => (a[data.prop] < b[data.prop] ? -1 : 1));
 }
},

but after the sort I would like to only show the objects that have tag === "house".

any ideas? thanks!

1
  • 2
    .filter(({tag}) => tag === 'house')? Commented May 26, 2020 at 11:25

1 Answer 1

1

You can use javascript array filter

const filteredProjects = projects.filter(({tag}) => tag === "house");

or put it on the computed properties

data: () => ({
  projects: [
    {prop: "year", tag: "house"},
    ...
  ]
}),
computed: {
    filteredProjects: function () {
      return this.projects.filter(({tag}) => tag === "house");
    }
  }

and then use it in your template

<template>
  <div>
    {{ filteredProjects }}
  </div>
</template>
Sign up to request clarification or add additional context in comments.

2 Comments

projects.filter( ({tag}) => tag === "house") thats shorter syntax
thanks for the help! i have some errors, but i guess im doing something wrong. i'll prepare a jsfiddle

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.