Mastering the art of iterating through an array of objects in Vue/Nuxt

<template>
  <div>
    <h1>Greetings from the World</h1>
    <div>
      <span :for="day in days">{{ day }} </span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Greetings',
  data() {
    return {
      days: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
    }
  },
}
</script>

I seem to be having trouble iterating through my days array. The error message I am receiving is displayed below.

Error: [Vue warn]: Property or method "day" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

Answer №1

Check out this related query: How to properly loop through an array and display data using Nuxt with v-for

Just like in that example, I highly recommend assigning unique IDs for correct handling of :key to avoid any ESlint issues.

<template>
  <div>
    <h1>Greetings Universe</h1>
    <div>
      <span v-for="day in days" :key="day.id">
        {{ day.name }}
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Hello',
  data() {
    return {
      days: [
        { id: 1, name: "Monday" },
        { id: 2, name: "Tuesday" },
        { id: 3, name: "Wednesday" },
        { id: 4, name: "Thursday" },
        { id: 5, name: "Friday" },
        { id: 6, name: "Saturday" },
        { id: 7, name: "Sunday" },
      ]
    }
  },
}
</script>

:key is crucial, learn more here: https://v2.vuejs.org/v2/style-guide/#Keyed-v-for-essential

This blog post offers insight on the topic:

Answer №2

It is recommended to utilize the v-for directive in the following manner:

<span v-for="day in days">{{ day }}</span>

The directives start with v- and are automatically linked to the component's methods and attributes.

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 for loop finishes execution before the Observable in Angular emits its values

I am currently using a function called syncOnRoute() that returns a Promise. This function is responsible for synchronizing my data to the API multiple times based on the length of the data, and it returns a string message each time. However, I am facing a ...

Utilizing AngularJS to implement a Currency Filter on the output of a personalized filter

I'm currently working on a custom filter that transforms zero values into ' - ' for display purposes. The goal is to display currency formatting for non-zero values. However, I encountered an unexpected token error while trying to implement ...

Having trouble disabling JavaScript with Selenium

I'm attempting to disable JavaScript through the Selenium profile when launching a browser. This method has worked in the past, but after updating my Selenium/Firefox versions, I am encountering difficulties. profile = webdriver.FirefoxProfile() ...

Can you reference document.styleSheets[].cssRules[] by specifying a rule name?

I am trying to modify a specific class property value in JavaScript without using an integer as a pointer or looping through all classes to find a matching selectorText value. This is specifically for changing the language displayed on the webpage. <s ...

The second pop-up modal fails to appear

I have successfully implemented 2 modal windows with the help of bootstrap. The first modal is used for adding a user to the database, and the second one is meant for editing an existing user. While the first modal works perfectly fine, the editing modal d ...

Surprisingly stumbled into the 'restricted' directory while switching the current directory to a temporary folder

I've encountered a strange issue while attempting to create and switch the working directory to a temporary folder using Node.js. Check out the code snippet below: var path = require('path') var fse = require('fs-extra') var TEST ...

The error message in React Hook Form states that the handleSubmit function is not recognized

I'm facing an issue with using react-hook-form for capturing user input. React keeps throwing an error that says "handleSubmit is not a function". Any suggestions on how to resolve this would be highly appreciated. Here's the code snippet I&apos ...

Animating the scaling of a cube along the Y-axis using WebGL and Three.js

Experiencing Unexpected Results with Tween in Webgl Three.js and JavaScript var scale = {y:0}; var tween = new TWEEN.Tween( cube.scale.y ).to( { y: 10 }, 3000 ).easing( TWEEN.Easing.Cubic.Out ).start(); I am attempting to animate the stretchi ...

Changing the 'badge' to 'panel' within the UI framework of 'ant design' has been set

Can the Badge be placed next to 'Info' in a Panel using ant design? https://i.sstatic.net/Lldc7.png View Code <div> <Collapse> <Panel header="Info" key="1"> <Badge count={4} style={{ b ...

Exploring the world of HTML and Javascript to enhance user search functionality

When I type in the search form, JavaScript is returning an error: "Uncaught TypeError: Cannot read property 'innerText' of null". I am working on a page with Bootstrap cards and trying to filter them by name. I attempted to access the "card-titl ...

Freezing objects using Object.freeze and utilizing array notation within objects

Could someone kindly explain to me how this function operates? Does [taskTypes.wind.printer_3d] serve as a method for defining an object's property? Is ["windFarm"] considered an array containing just one item? Deciphering another person& ...

How can I place a DOM element without relying on style properties?

Before diving in, let me provide some context. I am currently developing a visual element that operates similar to a spreadsheet. It features scrollable content areas in both directions, with axis indicators on the left and top that remain visible at all t ...

Modifying column array properties using jsstore

I am working with a jsstore table called tblInvoice const tblInvoice: ITable = { name: "invoice", columns: { // Here "Id" is name of column id: { autoIncrement: true, primaryKey: true, notNull: false }, branchId: { ...

Javascript beginner attempting to grasp the DOM using AJAX, feeling perplexed (conceptual)

I'm a data guy who dabbles in Python and web development, but lacks understanding of JavaScript and the DOM. Recently, I encountered a strange issue that prompted me to seek clarity on the mechanics behind it. The problem scenario: There are numerou ...

How can JSON data objects be transmitted to the HTML side?

I created a JSON object using Node JS functions, and now I'm looking to transfer this data to the HTML side in order to generate a table with it. However, I'm encountering difficulties sending the JSON object over because of my limited experience ...

Utilizing ReactJS to dynamically display various content based on onclick event

Is it possible to dynamically display different content based on button clicks? I want to create a schedule where clicking on specific days like Monday will reveal exercises and timings only for that day. The same goes for Thursday and other days. You ca ...

Rendering textures in Firefox using Three.js

I am currently working on creating a plane using three.js and applying a texture to it. The texture itself is generated from a canvas element. Interestingly, I have encountered some compatibility issues with Firefox specifically, as other browsers like IE ...

Invoke a JavaScript function with arguments upon clicking on a hyperlink

Struggling to generate code for an href tag with a JavaScript function that takes parameters - a string and an object converted into a json string. My initial attempt looked like this: return '<a style="text-decoration:underline;cursor:pointer" ta ...

Guide on obtaining an access token for an API through the use of the POST method in JavaScript

I am trying to obtain credentials for an API that uses OAuth2. The API documentation outlines the process as follows: Request access token: POST: auth/access_token Url Parms: grant_type : "client_credentials" client_id : Client id clien ...

Encountering an undefined value from state when implementing useEffect and useState

One issue I am facing is that the state of my projects sometimes returns as undefined. It's puzzling to me why this happens. In the useEffect hook, I have a function that fetches project data from an API call to the backend server. This should return ...