The v-for loop is looking for a numerical value, but it received something that is Not

The v-for directive is having trouble accessing the nStars prop in order to run a loop. I am attempting to display multiple stars by using the component <display-stars>. However, the component does not seem to be receiving the nStars prop for the loop to execute. Here is my code:
HTML

<h1><display-stars :nStars="3"></display-stars></h1>

Logic

  const app = Vue.createApp({}); // It contains more logic, but it's not relevant.
  app.component("star", { template: `<i class="fas fa-star"></i>` });
  app.component("empty-star", { template: `<i class="far fa-star"></i>` });
  app.component("display-stars", {
    props: ["nStars"],
    template: `<div class="stars-container">
     <star v-for="i in nStars" :key="i"></star> <empty-star v-for="j in (5-nStars)" :key="j"></empty-star>
    </div>`,
  });

  const componentInstance_vm = app.mount("#app");

I attempted to consult the Vue docs on how to pass props, and also looked at this Stack Overflow post about using v-for with an integer instead of a collection. Perhaps I overlooked something as I am new to Vue 3 coming from a background in React.

Answer №1

nStars represents a Number. It's important to note that you can only use v-for with arrays or objects in Vue.

If you need to create an Array with 3 elements, you can achieve this using Array.from:

let arr = Array.from({length: 3});

This will result in

[undefined, undefined, undefined]
. (Keep in mind that you can also create arrays with non-undefined elements. See MDN for more information.)

To loop through nStars times in Vue, use the following code:

<star v-for="(o,i) in Array.from({length: nStars})" :key="i"></star>

Here, i represents the index of each iteration.

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

What is the process for automatically importing unplugin-icons?

After creating a JavaScript project using vue-cli, I encountered an issue with auto-importing ElementPlus and Icons, causing the project to not run properly. How can I resolve this issue and get the project running smoothly? The package.json looks like th ...

unable to establish connection due to port error in node.js

While executing node app.js I encountered the following error message info - socket.io started warn - error raised: Error: listen EACCES This snippet shows all the JavaScript code within the application. After running sudo supervisor app.js T ...

Turn off auto-suggestion feature on the iPad and iPhone keyboard using JavaScript

I'm facing a challenge where I want to turn off the predictive suggestions and autocorrect feature on the iPad keyboard. This image is just for display purposes and not from my actual project. I need to hide the highlighted suggestion bar, and I am u ...

Failure to append an object to the array occurs when submitting an HTML form

I am facing an issue with a form that is supposed to add input values to an array as objects when submitted. However, every time I submit the form, the console briefly shows that there is an array with one object, only for it to disappear. Below is the f ...

A child component in Vue.js unexpectedly starts updating when the parent component renders and uses object literals as props

I'm currently facing an issue with my Vue components where object literals are passed as props like: <child :prop1="{ foo: 'bar' }"></child> After rerendering the parent component, the prop prop1 changes and triggers an update ...

Deactivating the class from a button

On my website, I have 3 buttons that represent different product categories. The initial state of the page should load with the "All Products" button having an active class. However, when clicked, this active class should be removed from the "All Products" ...

Display a DIV next to the mouse cursor when a span is hovered over

Is there a way to make a DIV element appear at the mouse cursor when a user hovers over a SPAN or another DIV? I attempted to create a function for this purpose, but unfortunately, it does not seem to be working properly even though jQuery is correctly lo ...

No response from jQuery's $.getJSON() function

I'm currently experimenting with jQuery by using a script that I wrote. As a beginner in learning jQuery, I am trying to read data from a .json file and display it in a div. function jQuerytest() { $.getJSON( "books/testbook/pageIndex.json", func ...

Utilizing Moment.js: Transforming 12-hour format to a Date object

Is there a way to convert a 12-hour string into a 24-hour Date object? day.from = day.from || moment("6:00", ["h:mm"]).format("HH:mm"); Unfortunately, I am encountering the following error: angular.js:11706 Error: [ngModel:datefmt] Expected `6:00` to be ...

The form data for a Laravel Vue.js/Axios PUT request is coming through as empty

I am facing an issue with sending data using axios. When I use a post request to send formdata, the data is successfully sent to the API. However, when I switch to using a put request, it seems that formData is not being properly handled. <template> ...

"Utilizing jQuery AJAX to enable multiple file uploads with specific file extensions

I've been facing an issue with uploading multiple files using jQuery and AJAX. The problem arises when I attempt to submit a PNG or JPG file, but it fails to submit and instead alerts me to "Please Upload File" even though a file has been selected. ...

Vue.js <v-data-table> - Automatic sorting/ custom sorting options

I am trying to arrange the numerical data in a Vue.js data-table in descending order right from the start. I want it to look like the screenshot provided below. Screenshot of My Desired Result The data that needs to be arranged in descending order is the ...

Error in line 36: firebase.auth function is undefined

Snippet of index.html code <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" ...

Auto-scroll feature malfunctioning

My auto scroll function using jQuery isn't working, here is my CSS: #convo_mes{ text-align:left; width:98%; height:80%; background:#fff; border:1px solid #000; overflow-x:auto; } And in my JavaScript: $(".mes").click(functio ...

Tips for adding extra spacing between icon and text field using Vuetify

The code snippet below is used for the username section: <v-text-field prepend-icon="fas fa-user" height="60px" placeholder="Username" outlined ></v-text-field> Is there a way to add space between the user ...

Arrange, Explore (Refine), Segment HTML Chart

Is there a way to efficiently sort, paginate, and search (based on a specific column) data in an HTML table? I've explored several JQuery plugins such as DataTable, TableSorter, Picnet Table Filter, but none seem to offer all three functionalities (se ...

NodeJS presents a potential maze of confusion with its promises

I've been struggling to grasp the concept of asynchronous code execution in NodeJS. My goal is to fetch the output of ip a on a Linux machine and extract the IP Subnet manually. Once that is done, I simply want to use console.log() to display the IP S ...

Discovering the solution to populating and building a tree structure using jsTree in conjunction with SQL Server, addressing the challenges associated with the

My current challenge involves using JSTREE to display a list of system modules. The issue arises from the fact that, according to the jsTree documentation, I need to use # in my query to create the tree structure. However, when I execute the following quer ...

Guide to dynamically inserting an audio file into a div with jQuery

I am looking to dynamically insert an audio file. Below are the declared tags: <div> <audio id="myaudio"> </audio> </div> Now, I am trying to add the source dynamically. Can anyone help me with how to add it to the div or audi ...

Building a query in Javascript by utilizing object keys and values: a step-by-step guide

I am looking to transform an object with various keys and values into a query string format, for example: obj1: { abc: "Abc", id: 1, address: "something" }. The challenge is that this object is generated dynamically, so the numbe ...