Sort the Vue.js array by year and search for items by tag

In my VueJS project, I am working with data fetched from an API using axios. The data consists of projects with various properties such as year, location, and tags like house or park. I have implemented a filtering system to sort the projects based on a specific property:

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));
 }
},

However, after sorting the projects, I now want to display only those objects that have the tag equal to "house". Can anyone suggest how I can achieve this? Thank you!

Answer №1

If you're looking to filter arrays in JavaScript, there are a couple of ways you can do it.

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

You could also consider using computed properties for this purpose.

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

Once you have set up the filtering logic, you can easily display the results in your template.

<template>
  <div>
    {{ filteredProjects }}
  </div>
</template>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mapping JSON data to a HashMap

I pull data from a JSON file in the format: "B11, B12, B22, F11, F22, F1, F2, F3." After receiving this data, I have a layout with 50 icons. My goal is to make 8 icons VISIBLE and the remaining 42 icons INVISIBLE. I thought about using a HashMap for this ...

Saving various data types in Java programming language on Android devices

Here's a question that could be worth a million dollars for someone like me who is just starting out. How can I store a list in XML using Java for Android, similar to this example: <?xml version="1.0" encoding="utf-8"?> <places> <i ...

Which option is more suitable for my needs: utilizing an array or an sqlite database

I am in need of guidance as I work on creating an app. Although I have no prior experience with android app development, I am dedicating time to studying and practicing. The goal is to develop an app that allows users to record payments from a customer li ...

Having trouble displaying information in JavaScript after using getScript() to retrieve data from an API containing a JSON block database

I'm a newcomer to the world of Ajax/json/jquery and I find myself with a few inquiries. Currently, I am working with an API located at which contains a JSON block that looks something like this [{"id":"1","FirstName":"Micheal","LastName":"Kooling"}, ...

Checking the status of a checkbox after submitting using AngularJs

For my first application, I am utilizing AngularJs and JavaScript to display information from an API as checkboxes. Currently, the functionality is working well, but I am unsure how to validate if any checkbox options are checked with a submit button. Aft ...

execute the function within the data() method

I'm currently working on retrieving data for my search tree, and I'm facing issues with fetching the data directly from axios or calling a function because it cannot be found. export default { name: 'SideNavMenu', data () { return ...

An issue has been encountered in NodeJS with a route that begins with a percent sign causing an error

I have a NodeJS server set up with the following backend configuration: app.use(express.static(__dirname)) app.get('/', function(req, res){ res.send('main page here') }); app.get('/*', function(req, res){ res.send(&apos ...

Vue.js provides an interesting object that contains observed data

My axios request looks like this: retrieveData() { Axios.get( '/vue/get-data/', { ...

Using jQuery to create a drop-down menu

I currently have a table filled with data. Whenever a user clicks on a cell within a specific column, I would like it to transform into a select dropdown menu. This dropdown menu will allow the user to choose a category for that particular row. The selecte ...

Trouble with Basic JavaScript Comparison Function

I'm facing an issue with a JavaScript comparison that doesn't seem to be working. It's puzzling why it's skipping to line 12302 and setting showNoDataText = true. The logic dictates that it should be false since the array length of 285 ...

Running tests in jest with or without mocks depending on the condition

Currently, I am performing tests using mocks to avoid always hitting APIs. Nevertheless, I also aim to introduce a condition that would allow me to utilize the same test for API testing purposes. However, when I include a condition, it seems to be disrega ...

Removing Content with React

I attempted to remove the item, but when I click on the button the function does not work! Deleting in the backend is functional, however it does not work in the frontend! This is the delete function: deleteblog = async id => { // this.setSta ...

What is the best approach for retrieving Google API responses - accessing them directly from the frontend or routing through the backend first?

Currently, I am in search of the optimal location to make Google API requests. Given that Google Maps is utilized in the front end, we have the option to directly call the distance API service from there. However, we also have the choice to access these se ...

Images loaded from Firebase are only showing as text in the RecyclerView fragment, rather than displaying as actual images

My images from Firebase are not appearing in the image view, although the name of an image is visible in the text view above the recycler view. Here's my code that I'm struggling with. package com.example.bbeast.HomeActivity; import android.n ...

Tips for implementing an overlay navigation menu specifically for mobile devices, with a standard navigation bar for larger screens

I'm currently working on creating a unique full screen overlay navigation menu, but I've encountered some challenges along the way. My goal is to have the overlay appear underneath my transformed hamburger menu and hide all other elements on the ...

The issue with ng-if not functioning within ng-repeat is that the ng-if directive

My issue is with using ng-if inside an ng-repeat in AngularJS. Despite updating to the latest version on 09/27/2014, I am still unable to make it work properly. The code functions perfectly outside of ng-repeat, and also works fine inside ng-repeat when us ...

Tips for showcasing an uploaded image with ajax

I am looking to upload an image and display it without having to reload the page. I believe this can be achieved using Ajax form submission. However, I have tried some code but the Ajax form submit function does not seem to be working for me. Can someone p ...

Adding objects from an existing JSON file to an empty object in Node.js is a straightforward process

Looking to extract JSON data from a separate file and transfer it into a new object using Node JS. Despite trying various methods without success, I still can't figure out how to achieve this. Can anyone provide guidance or suggestions? Add new eleme ...

Ways to extract data from a chosen radio button choice

Excuse me, I have a form with an input field and radio buttons. Can someone please help me with how to retrieve the value of the selected radio button and then populate that value into the input text field? For reference, here is the link to the jsfiddle: ...

Employing setTimeout for hover interactions in conjunction with a tooltip

I have created an SVG map consisting of hexagons grouped together. My goal is to display a tooltip when the user hovers over a group, but with a 3-second delay. If the user moves away before the delay ends, I want to clear that delay to prevent the tooltip ...