Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do i get the total array length in vuejs

i’m trying to get the total result length but i’m not getting anything in my template
this my script

  data() {
    return {
      searchResults: [],
      totalResults: [],
}}
       const response = await axios.post(
          "http://localhost:5000/api/search",
          searchData
        );
        this.searchResults = response.data.Response.Results; // Set the search results in the component's data       // Retrieve the traceId from the response
        const nestedResults = response.data.Response.Results;
        const totalResults = nestedResults[0].length;
        console.log("Total Results:", totalResults);

this is my console i’m getting totalResults

Total Results: 12

this my template

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 <p>Total Results: {{ totalResults }}</p>

template returns this

Total Results: []

i dont get anything in my template please how can i go about this

>Solution :

First off you’re initializing a variable that should be a number, with an empty array. You should have:

data() {
    return {
      searchResults: [],
      totalResults: 0,
}}

Second of all you’re not assigning the value to the correct totalResults, you’re simply declaring a new variable. To assign the value you should use this.totalResults. Therefore

this.totalResults = nestedResults[0].length;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading