Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
v-for and v-if causing issues with an undefined variable

In my Vue application, I have a very small line of code based on a v-for loop and it works until I throw a V-IF into it.

THe following works:

<div v-for="date in dates" :key="date">
  <th v-for="store in stores">@{{store.stock}}</th>
</div>

However, when I try to get it to only show that value if the date in the object matches the dates object, I get that store is undefined

<div v-for="date in dates" :key="date">
  <div v-for="store in stores">
  <th v-if="store.date === date">@{{store.stock}}</th>
  </div>
</div>

Here are my objects:

stores: [
  {
    store: "123",
    date: "2021-09-01",
    stock: "145"
  }
]

dates: [
  {
    date: "2021-09-01"
  }

]

Why am I having such an issue when trying to match the dates in the v-if?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You made a little mistake. date should be date.date!

<div v-for="date in dates" :key="date">
  <div v-for="store in stores">
  <th v-if="store.date === date.date">@{{store.stock}}</th>
  </div>
</div>
about 4 years ago · Juan Pablo Isaza Report

0

instead of use v-if with v-for you can use computed to get store you need display and then use just one v-for

<template>
  <div>
    <div
      v-for="store in activeStore"
      :key="store.stock"
    >
      {{ store.date }}
    </div>
  </div>
</template>
<script>
export default ({
  data() {
    return {
      count: 1110,
      stores: [{
        store: 'test',
        date: '2021-09-01',
        stock: '145'
      }, {
        store: 'test',
        date: '2021-09-02',
        stock: '146'
      }, {
        store: 'test',
        date: '2021-09-03',
        stock: '147'
      }, {
        store: 'test',
        date: '2021-09-04',
        stock: '148'
      }],
      dates: ['2021-09-01', '2021-09-02', '2021-09-03', '2021-09-00']
    }
  },
  computed: {
    activeStore: function() {
      return this.stores.filter((store) => {
        if (this.dates.includes(store.date)) {
          return store
        }
      })
    }
  }
})
</script>
about 4 years ago · Juan Pablo Isaza Report

0

Here is a simple solution using computed property

<div v-for="date in dates" :key="date">
  <div v-for="(store, indexer) in getFilteredStores(date)" :key="indexer">
  <th>@{{store.stock}}</th>
  </div>
</div>

and in your computed, define the below handler to get the store for particular dates

computed: {
 getFilteredStores() {
   return targetDate => { // targetDate is the argument passed to the handler
    return this.stores.filter(store => store.date === targetDate);
   }
 }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!