Iterate through a modified list in Vue.js using a parameter

Is there a more efficient way to iterate through a filtered list with a given input parameter in vue.js?

I want to avoid using filtering within the template like

<li v-for="x in todos_filtered" v-if="x.person == 'Mike'">
when dealing with a large number of records.

Below is the code that is returning an empty list instead of the expected 2 elements.

<html>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583d28283e293f283c26753b"><strong>[...]</strong></a>/dist/vue.js"></script>
<body>

<div id="app">
  <ul>
    <li v-for="x in todos_filtered('Mike')">
      {{ x.text }}
    </li>
  </ul>    
</div>

<script>
myObject = new Vue({
  el: '#app',
  data: {  
    todos: [
      { person: 'Mike', text: 'Learn JavaScript' },
      { person: 'Luke', text: 'Learn Vue.js' },
      { person: 'Mike', text: 'Build Something Awesome' }
    ]
  },
  computed: {
    todos_filtered(person) {
      return this.todos.filter((x) => x.person === person);
    }
  },
})
</script>
</body>
</html>

Answer №1

To create the computed property, it is important to define a function that accepts the person as an argument:

  computed: {
    filteredTodos() {
      return (person) => this.todos.filter((item) => item.person === person);
    }
  },

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

Monitor user inactivity with JavaScript

Our website contains iframe links to various content sites where users can take online exams. Some of these exams on the content pages run for over 3 hours. Additionally, an exam guard feature is implemented to prevent users from engaging in other activiti ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

Executing Function Upon Clicking an Option in Datalist

I'm facing an issue with datalist in my VueJS search input. Why isn't my function getData(), which is triggered by both @click and @keypress.enter, being called when I select an option from the datalist using mouse or enter key? Below is the cod ...

How can I retrieve values of selected checkboxes using the Express Data API?

I have a scenario where I need to retrieve data from input checkboxes only when the checkbox for my express post function is selected. There are 3 checkboxes with values of 1000, 2000, and 3000 as follows: <input type="checkbox" name=" ...

Sign up a new user using Passport JS

Looking to utilize passport.js for adding a new user from the signup page. The signup form is as follows: <form id="Signup-form" name="SignupForm" action="/signup" method="post"/> <input type="text" id="firstname" name="Firstname" > <input ...

Securing your single page application with Firebase and .Net WebAPI 2 authentication

I have a Single Page Application built with AngularJs (although the framework is not crucial at this stage). The application is hosted on IIS and consists of an index.html file along with various client assets. For the backend, I am using WebApi 2, which ...

Nested iteration using a for loop in Javascript

I am attempting to iterate through a large array of values and calculate the average of one of the values for each second. I seem to be having trouble getting this code to function correctly, and it appears that the issue lies within the nested while loop. ...

Switching out the background image with a higher resolution one specifically for users with retina devices

I'm trying to create my very first website that is retina ready, but I've run into an issue when it comes to updating images to higher resolutions in CSS. I'm unsure how to go about having a standard image as the background and then switchin ...

Manipulating nested arrays using index values in JavaScript

Can someone assist me in sorting a multidimensional array based on the value of the first index? I've tried using a for loop without success. Looking for solutions in JS or jQuery. I want to convert the following array: var pinData = [ ['< ...

Ways to transfer a variable from JavaScript/jQuery to Java

I am trying to access data retrieved from another server in a Java class using JavaScript/jQuery. The data I receive from the server as a post result to my servlet is in the form of a JSON String. The response looks like this: [{"id":"1","bool_m":"0","na ...

Tips for explaining the structure of a basic Just functor in TypeScript

I am embarking on my first attempt to create a simple interface in TypeScript, and I find myself questioning every step along the way. The core question that troubles me is: How can I best describe this straightforward Jest matcher extension? /** * @par ...

Deactivate the inputs corresponding to the option selected in a given row, with this rule applying uniformly to all rows

I need assistance with modifying a table where rows are duplicated from the original, resulting in all IDs being the same. How can I go about disabling an input based on the option chosen within the same row? For instance, if I select option 10 in the sec ...

Web Inspector shows no trace of HttpOnly Cookies

My current project involves implementing user authentication for a website using the MERN stack, and I have opted to utilize JWT tokens stored as HttpOnly cookies. Interestingly, when I made the request via Postman, the cookie was sent in the "Set-Cookie" ...

Removing a custom object with a click event in Three.JS

I need help with implementing two functionalities: 1) SHIFT + click on an object to remove it from the scene, and 2) a clear button to remove all objects from the scene. Check out the live version here: https://cs12students.dce.harvard.edu/~amarkham/sandb ...

Encountering an error in Jest with TypeScript (Backend - Node/Express) that reads "Cannot use import statement outside a module

Currently, I am in the process of developing Jest tests for a Node/Express TypeScript backend. Recently, I came across the concept of global test setup which I am integrating to streamline the usage of variables and function calls that are repeated in all ...

Unable to modify the content of a table cell after triggering a Bootstrap modal event

Looking to update values in rows of a table? Here is an example table: https://i.sstatic.net/6l4f3.png I need to update the title per selected row. After selecting the first row, changing the value works fine. However, when I choose the second row, the ...

Personalize the error message for throwing an exception in JavaScript

I've been working on customizing error messages for exceptions thrown in JavaScript. Despite my best efforts, I have not been successful so far. I'm currently attempting the following code snippet but it's not functioning as expected: f ...

Tips for utilizing v-model with props within a v-for loop in Vue 3

I am trying to implement v-model with props inside a v-for loop. I have successfully used v-model with props in the past using computed variables, but I am unsure how to achieve this within a v-for loop. Here is my current code: <script setup> const ...

jQuery issue: Inability of "Read More" span to reappear after clicking "Read Less"

Currently in the process of creating a portfolio website that showcases project descriptions with an excerpt, revealing full details upon clicking "Read More." My experience with jQuery is limited, but I managed to implement functionality where clicking "R ...

Tips for choosing a loaded element using the jQuery load() method

I am currently facing a challenge with the following (here is a snippet of code to illustrate): <div id="container"></div> <script type="text/javascript"> $('#container').load('content.html'); $('.eleme ...