Using VueJS to iterate through an array while applying a condition to filter out certain elements (combining v-for with v-if)

I'm currently working on a v-for loop to display two columns of card elements. The idea is to print the current element and the next one in a row if the index is even, skipping the elements with odd indexes.

This is what my code looks like:

<template>
    <q-layout>
        <div v-if="!showBack" class="card scroll" id="cards-view">
            <div class="layout-padding">
                <p class="group">
                    <button class="primary circular fixed-bottom add-btn"><router-link to="/create" exact><i class="icon-32 text-white">add</i></router-link></button>
                </p>
                <div class="row content-center text-center gutter" v-for="(index, pet) in pets" v-if="index % 2 === 0">
                    <div class="auto ">
                        <div class="shadow-1">
                            <img class="responsive" :src="pets[index].name">
                            <div class="card-content text-bold">
                                <img class="responsive sex" :src="pets[index].sex">{{ pets[index].name }}
                            </div>
                        </div>
                    </div>
                    <div class="auto ">
                        <div class="shadow-1">
                            <img class="responsive" :src="pets[index+1].name">
                            <div class="card-content text-bold">
                                <img class="responsive sex" :src="pets[index+1].sex">{{ pets[index+1].name }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <router-view class="layout-view"></router-view>

    </q-layout>
</template>

<script>
export default {
    data () {
        return {
            pets: [{
                name: 'Júpiter',
                sex: 'statics/img/female.jpg',
                photo: 'statics/img/jupiter.jpg'
            },{
                name: 'Ringo',
                sex: 'statics/img/male.jpg',
                photo: 'statics/img/ringo.jpg'
            }]
        }
    }
}
</script>

However, I encountered an error that says:

[Vue warn]: Error when rendering anonymous component at...

vue.runtime.common.js?d43f:435 TypeError: Cannot read property 'photo' of undefined at eval (eval at 167 (0.cd4853d….js:28), :95:31)...

Essentially, I'm looking for a way to include a conditional statement within my array loop. Any suggestions on how I can accomplish this?

Answer №1

Make a simple modification:

v-for="(index, item) in list"

Change it to:

v-for="(item, index) in list"

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

The alignment of Bootstrap v4.5 checkbox is limited as it cannot be positioned horizontally or vertically when placed next to an input field

As stated in the title: Bootstrap's checkbox is having trouble aligning Horizontally or Vertically when placed next to an input. Error Displayed: https://i.sstatic.net/hNHpm.png I have experimented with various solutions, but none have provided sat ...

Update vue-table-2 to activate search filter upon button click, rather than keystrokes

exploring the capabilities of https://github.com/matfish2/vue-tables-2 using Vue.js version 2.6.11 Hey there! I'm a newbie developer delving into Vue.js after working in the React world. I'm aiming to modify the filter/search functionality to tr ...

Instantly display selected image

I have encountered some challenges with my previous question on Stack Overflow as I couldn't find a suitable solution. Therefore, I decided to explore an alternative method for uploading images. My current goal is to upload an image immediately after ...

Interactive form found on a webpage

Hey there! I'm currently working on a task where I want a form to be displayed when the edit button is clicked. Once the save button in the form is pressed, I want to update my database with the new information. It's crucial that this process hap ...

Is it possible to modify the object's key value when generating an array value with the map function in React?

I have the array object data stored in a variable called hi[0].child. hi[0].child = [ {code: "food", name: "burger"}, {code: "cloth", name: "outer"}, {code: "fruit", name: "apple"}, ] ...

Encountering MIME type error (text/html) during Angular project deployment

I am facing an issue while trying to deploy a project built with Angular/CLI 6.12.0. After transferring the content of the "dist" folder to a server, I encountered a console error related to MIME type. The module at address "http://www.sylvainallain.fr/p ...

Trigger the reactive helper again when there is a change in the $scope

I am working with a reactive data source in Angular-Meteor and I want to find a method to trigger the reactive function to run again after modifying another $scope value. Specifically, I aim to change the sorting order based on $scope.model.sort: angular. ...

Adjustable height for text input field

Is it possible to adjust the height of a textarea dynamically based on user input, rather than relying on scrollbars when content exceeds the width and height of the field? <textarea class="form-control"></textarea> For instance, if a user ty ...

What could be the reason for Angular to merge the element at index 0 of an array into a subarray instead of doing

After setting up the Array in my oninit function, I encountered an issue where one part of the array was functioning as intended while the other returned an error. this.tests = [{ status: 0, testresults: [{ name: 'test ...

Are the elements in the second array the result of squaring each element in the first array? (CODEWARS)

I am currently working on a 6kyu challenge on codewars and I've encountered a frustrating issue that I can't seem to figure out. The task at hand involves comparing two arrays, a and b, to determine if they have the same elements with the same mu ...

What could be causing the failure to retrieve the salt and hash values from the database in NodeJS?

My current issue involves the retrieval of hash and salt values from the database. Although these values are being stored during sign up, they are not being retrieved when needed by the application. Below, you will find snapshots of the database, console s ...

Relocating scripts that have already been loaded

When using AJAX to load a page, the entire content including <html>, <head>, <body> is loaded. This means that all scripts meant to run on page load will be called. However, sometimes the browser may remember that certain scripts have alr ...

Getting Session from Next-Auth in API Route: A Step-by-Step Guide

When printing my session from Next Auth in a component like this, I can easily see all of its data. const session = useSession(); // ... <p>{JSON.stringify(session)}</p> I am facing an issue where I need to access the content of the session i ...

Using Accordions in Jquery to dynamically adjust page height during ajax calls

I am currently using AJAX to call in a page and animate its height successfully. However, I have encountered an issue with an accordion-like function that is supposed to toggle the visibility of an element and adjust the height of the containing element ac ...

`To activate/deactivate tabs by choosing options from drop-down menus`

Hey there, I'm currently dealing with a combo box that has 5 drop down items in Tab1. There are also other tabs present such as tab2, tab3, tab4, and tab5. Tab1 is enabled while the other tabs are disabled. Each of the disabled tabs contains different ...

The option list in AngularJS is cleared when an option is selected

In my current project, I am developing a django-tastypie api application with angularjs as the JavaScript framework. The main part of this application involves managing curriculum objects, each containing a list of grade objects and each grade object furth ...

What is the best method for choosing a div element using JavaScript and altering its background color?

On my website, I have a pop-up modal with a form that contains 5 divs, each with a picture and description. I came across this JS code on w3schools that allows me to style a selected div. However, when the modal is opened, one of the divs is already pre-se ...

Content will be loaded only after the user clicks on the item

On my blog, I have a share button that displays different social media platforms when clicked. However, the page takes longer to load due to fetching data from multiple servers like Facebook and Twitter. Is there a way to use jQuery to make these buttons l ...

Resolving CORS Issue with Passport in Node.js

I have exhausted all efforts to integrate passport into my application without success. Every login attempt with various providers (Facebook, Google, Twitter, Microsoft) has resulted in an error message similar to this: XMLHttpRequest cannot load https:// ...

Encountering a cannot POST / error while using Express

I am encountering an issue with my RESTful API while using Postman to call the route /websites. Despite my efforts, I keep receiving the error message "Cannot POST /websites". In addition, I am exploring the implementation of a job queue utilizing Express, ...