How to retrieve the length of an array using

Currently, I am deepening my understanding in javascript and exploring the vue.js 3 composition API. I have a specific question regarding retrieving the length of an array and displaying it within a <p> tag. The array in question is named "getForms".

<script>.....
const forms_length = computed(() => getForms.value.length)

<template>....
<p> {{form_length}} </p>

After implementing this code snippet, I encountered an error message: "Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')"

Could you please explain why this error occurs and what steps should be taken to resolve it?

Your assistance on this matter would be greatly appreciated. Thank you!

Answer №1

Check out this example of using the computed property:

<template>
  <p>Total items in array: {{ itemTotal }}</p>
</template>
<script>
  import { computed } from 'vue'
  import { useItemsStore } from '../store/items' 
  setup() {
    const { store } = useItemsStore()
    
   // If the store.items array is not defined or ready, it will return an empty array
    const getItems = computed(() => { return store.items || []})
  
    // Using computed property to calculate the length of the array
    const itemTotal = computed(() => getItems.value.length)

    return {
      itemTotal
    }
  }
</script>

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

Creating components and dynamic routing based on the current route

I'm in the process of creating "overview" pages for different sections within my app, each triggered from the root of that particular section. For example, localhost/hi should display the HiOverview component, And localhost/he should display the HeO ...

Tips for updating text in a freshly opened window using JQuery or JavaScript

I have a function that is triggered by a button click to open a new window or tab, display an alert, and update text. Here is the code snippet: function nWin(p) { var setStyle = "<style rel='stylesheet'>\ .vTop {\ ...

Removing a row from a table with VueJS

I am brand new to Vue and I'm struggling to understand this particular scenario. My task is to remove a row from my table when a button is clicked. Below you can see the code snippets from each file responsible for rendering my page. Main shopping ...

PhoneGap Troubleshooting: Device Plugin Malfunctioning

I'm having trouble getting the device plugin to work with my Cordova/PhoneGap project. Currently, I am using Cordova version 3.3.1-0.1.2. I followed the documentation and installed the plugin using the following command: C:\ProjectFolder>pl ...

Exploring the hover effects on Kendo charts and series

I have encountered an issue with my kendo chart. The series hover works fine when the mouse hovers over the serie, but I am facing a problem where the value in the line does not appear as expected. I am unsure of why this is happening. $("#chart1").data(" ...

Exploring the depths of nested arrays in Vue: Accessing

Due to the complexity of my array containing multiple data, I faced challenges uploading all of them. Thus, I have chosen to provide an image showcasing my array and objects below. https://i.stack.imgur.com/yPspG.png View <draggable :list="reservatio ...

Leverage variables in Ajax to retrieve the data of an HTML element

Seeking assistance in converting multiple lines into a single for loop. var optie1 = ""; if($('#form #optie1_check').is(':checked')) { optie1 = $('#form #optie1_naam').val(); } var optie2 = ""; if($('#form #optie2_ch ...

Modifying the input in V-for does not update the placeholder and input values

I'm currently working on a form for passengers, where I need to input the number of adults and children along with their ages. Initially, the childCount is set to 0 and the inputs for childAges are hidden. As I increase the child count, these inputs s ...

Generate SVG components without displaying them

Is there a way to generate a custom SVG graphic through a function without the need to attach it to any element? Can I simply create an empty selection and return that instead? Here is my current implementation: function makeGraphic(svgParent) { retur ...

Achieving endless image rotation using vue-kinesis

My current setup involves using vue-kinesis for animating an image. The component is functioning correctly, but the animation itself is being controlled through direct CSS. How can I adjust my configuration to have vue-kinesis handle the rotation instead? ...

What is the memory allocation for null values in arrays by node.js?

Continuing the discussion from this thread: Do lots of null values in an array pose any harm? I experimented with node.js by doing this: arr=[] arr[1000]=1 arr[1000000000]=2 arr.sort() However, I encountered the following error: FATAL ERROR: JS Alloca ...

Eclipse - enhancing outline view by utilizing require.js define(...)

My code is structured within the define(...) function in the following format: define(['angular'], function(angular) { function foo () { console.log("Hi") ; } function foo2 () { console.log("Hi") ...

Retrieving precise information from the backend database by simply clicking a button

As a new full stack programmer, I find myself in a challenging situation. The root of my problem lies in the backend table where data is stored and retrieved in JSON format as an array of objects. My task is to display specific data on my HTML page when a ...

React.js issue with onChange event on <input> element freezing

I am experiencing an issue where the input box only allows me to type one letter at a time before getting stuck in its original position. This behavior is confusing to me as the code works fine in another project of mine. const [name, setName] = useStat ...

In React conditional return, it is anticipated that there will be a property assignment

What is the optimal way to organize a conditional block that relies on the loggedIn status? I am encountering an issue with a Parsing error and unexpected token. Can someone help me identify what mistake I am making and suggest a more efficient approach? ...

Embarking on a New Project with Cutting-Edge Technologies: Angular, Node.js/Express, Webpack, and Types

Recently, I've been following tutorials by Maximilian on Udemy for guidance. However, I have encountered a roadblock while trying to set up a new project from scratch involving a Node/Express and Angular 4 application. The issue seems to stem from the ...

Launching the file explorer when the icon is clicked

I am currently working with Angular 5 using Typescript. I need assistance in opening the file explorer window to add an attachment when clicking on an icon. I have successfully done this for a button, but I am facing issues with the click event binding on ...

Display data in Bootstrap table using JQuery and JSON

Can anyone help me figure out how to populate my bootstrap table (in Database.Master) with data (in DatabaseUI.aspx.cs)? I need to dynamically add rows to the table using JQuery. Do I need to convert my string to JSON first? I believe I may have to add an ...

How can I execute a task following a callback function in node.js?

Is there a way to run console.log only after the callback function has finished executing? var convertFile = require('convert-file'); var source, options; source = 'Document.pdf'; options = '-f pdf -t txt -o ./Text.txt'; ca ...

The orthographic camera method combined with raycasting for object selection

Currently, I'm facing a challenge when it comes to selecting objects with the orthographic camera using the raycaster. Interestingly, I don't encounter any issues when utilizing a perspective camera. The only difference between the two scenarios ...