Can someone explain why v-for is unable to display the computed value?

When selecting a city and area, the data should be filtered accordingly. However, I am facing an issue where the selected value does not appear.

I have tried various solutions and searched for similar code, but I have yet to find a resolution.

Below is an image of how the HTML renders:

https://i.stack.imgur.com/okvDE.png

Here is the template code:

<a href="#" class="list-group-item" v-for="(live , idx) of filterLivingNames" :key="idx">
    <h3 class="text-center">{{live.Name}}</h3>
    <p>Address:</p>
    <p>Phone:</p>
</a>

Script section:

import LivingData from '@/assets/LivingData.json';

export default {
    computed: {
        livedata(){
            return LivingData;
        },

        filterLivingNames(){
            let livelength = this.livedata.length

            for(let i = 0 ; i < livelength ; i++){
                if(this.livedata[i].Region === this.city && this.livedata[i].Town === this.area){
                    console.log(this.livedata[i])
                    return this.livedata[i]
                }
                else{
                    continue
                }
            }
        }
    }
}

Updated JSON file:

[
  {
    "Id": "1",
    "Name": "Hotel-1",
    "Region": "Region-1",
    "Town": "Town-1",
  },
  {
    "Id": "2",
    "Name": "Hotel-2",
    "Region": "Region-2",
    "Town": "Town-2",
  },
  {
    "Id": "3",
    "Name": "Hotel-3",
    "Region": "Region-2",
    "Town": "Town-1",
  },
  {
    "Id": "4",
    "Name": "Hotel-4",
    "Region": "Region-1",
    "Town": "Town-2",
  },
]

Answer №1

By filtering the livedata based on both Region and Town, you can efficiently solve the issue at hand using the Array.filter() method.

Check out this Live Demo:

new Vue({
  el: '#app',
  data: {
    livedata: [
      {
        "Id": "1",
        "Name": "Hotel-1",
        "Region": "Region-1",
        "Town": "Town-1",
      },
      {
        "Id": "2",
        "Name": "Hotel-2",
        "Region": "Region-2",
        "Town": "Town-2",
      },
      {
        "Id": "3",
        "Name": "Hotel-3",
        "Region": "Region-2",
        "Town": "Town-1",
      },
      {
        "Id": "4",
        "Name": "Hotel-4",
        "Region": "Region-1",
        "Town": "Town-2",
      }
    ],
    city: 'Region-1',
    area: 'Town-1'
  },
  computed: {
    filterLivingNames() {
      return this.livedata.filter(obj => obj.Region === this.city && obj.Town === this.area)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <a href="#" class="list-group-item" v-for="(livingData, index) of filterLivingNames" :key="index">
    <h3 class="text-center">{{ livingData.Name }}</h3>
    <p>Address:</p>
    <p>Phone:</p>
  </a>
</div>

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

Is there a way to customize the total time out for the v-progress-loader-circular?

After attempting to adjust the time value to 30 seconds and then resetting it, I found that the circular progress would always stop at 100. My goal is for it to count up to 30, with 30 being the maximum count. Even though I did manage to reset it after 30 ...

Tips for personalizing error messages for the "required" field by utilizing a dictionary feature on VeeValidate in Vue.Js

I am trying to update the error message that appears when an input field with the "cpf" rule is left empty (meaning it does not meet the "required" rule). I believe using the "dictionary method" with custom messages is the solution, but I am struggling to ...

VueJS throwing an error due to failed type check on prop

Using a component called CardRenderer.vue to render card using an array of Objects has been quite challenging for me. Repeatedly, I have used the same component to render the data and encountered this error: "[Vue warn]: Invalid prop: type check failed for ...

A guide to validating a v-edit-dialog within a v-datatable

As I navigate my way through vue.js and vuetify, I am faced with an issue regarding the validation of input within a v-edit-dialog located inside a v-datatable. Despite having functional validation in place, the save button remains enabled and accepts inva ...

Utilize a global variable in Vue.js

How can I efficiently pass a javascript variable globally to Vue components upon instantiation so that each registered component has it as a property or it can be accessed globally? Please note: I need to ensure that this global variable for vuejs is set ...

Error encountered when attempting to create a new Vue project using Vue CLI (npm issue)

I'm having trouble creating a new Vue CLI project. It was working fine before, but now it's not working... Vue CLI v4.2.3 ✨ Creating project in /Web develop/Vue Projects/new. ...

Vue loading all the pieces in one go on the initial request

Currently, I am delving into the world of Vue and just started learning about routing. After setting up a template project using vue/cli with an initial router configuration, I came across this Router code: export default new Router({ mode: 'histor ...

Leveraging unplugin-vue-components within a monorepository

Situation In my monorepo (Using Turborepo), I have multiple Vue3 apps and packages. Each app is built with Vite and uses unplugin-vue-components for importing components automatically from the specific app. Additionally, I need to include components from ...

Tips for inserting a hyperlink on every row in Vuetables using VUE.JS

Trying to implement a link structure within each row in VUEJS Vuetables has been challenging for me. Despite my research on components and slots, I am struggling to add a link with the desired structure as shown below: <td class="text-center"> <a ...

Turn off Vuetify's v-touch functionality within a specific nested element

Currently, I am utilizing Vuetify and seeking a way to trigger certain code when the user swipes either left or right. Within my project, there is a container structured as follows: <div v-touch="{ left: () => performActionA(), right ...

What is the best way to showcase the data retrieved from axios in a Vuejs table?

I am new to Vuejs and encountered an issue while trying to display user information retrieved from API endpoints using axios. Below is the code snippet for fetching data: import axios from 'axios'; export default { data(){ return{ ...

Troubleshooting: My Vue component is not displaying data from the

I am currently in the process of developing a trivia creation tool. Right now, I am focusing on enhancing the functionality of the question editing page. The EditQAForm component is responsible for retrieving the specific question and its answers, then pop ...

Exploring the distinction between type-based and value-based props validation in VueJS

defineProps({ lastName: { type: String, required: true }, }); const props = defineProps({ lastName: String }) I have a simple question regarding the code snippets above. The first block of code defines an object and adds appropriate validat ...

Is there a way to access data dynamically during rendering and display the outcome?

Upon rendering the object below, I am able to implement something similar to: <div v-for="(card, groupIndex) in cards" v-bind:key="card.id"> <div> {{cards[groupIndex].group.length}} </div> </div> This code snippet provides a cou ...

Guide on sending an image/file from Vue.js to Laravel 7 through axios

I'm experiencing a problem when trying to upload an image file using vue component to a laravel controller. The issue arises when I click the submit button; the phone/address is saved, but the image name does not save. data(){ return{ ...

I have noticed in my procedures that the calculations are not performed until the second digit is entered into the input values

Warning: The value "NaN" entered is not a valid number. Please ensure it matches the required regular expression format: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)? When values are inputted into the quantity and rate fields, the expected ...

How do I disable the hover and click highlighting effect on a div in Vuetify using v-on in Vue2?

Currently, I have implemented a Vuetify VListItem in a NavigationDrawer with an on click listener that displays a menu in the div below. The menu is functioning properly - opening and closing as expected. However, it highlights on hover/click which I wou ...

How to update the selected autocomplete item in Vue using programming techniques?

Although I am still learning Vue, consider the following scenario: <v-autocomplete v-model="defaultUser" :hint="`User: ${defaultUser.username}`" :items="users" :item-text="item =>`${item.firstName} - $ ...

Having trouble getting Vue.js data to show up on the screen. I'm attempting to show a list of todos, but all that

I'm currently working on my App.vue file where I have set up the data for a todo list. However, despite creating an array of todos and attempting to display them, nothing is showing up on the screen. I'm at a standstill and could really use some ...

Vue fails to retrieve the accurate value of the option, instead displaying the text

Just started using Vue.js through CDN. Currently working with a select element: <select @change="setValue($event.target.value)"> <option>Pick</option> <option val="1">First Choice</option> <opt ...