Why is my Vue list not showing the key values from a JavaScript object?

I am struggling to utilize a v-for directive in Vue.js to display the keys of a JavaScript object in a list. Initially, the object is empty but keys and values are populated based on an API call. Here's an example of the data structure (I used JSON.stringify for visualization purposes, but the original object is named fullFileList):

fullFileList{
  "8-27.TXT.rtf": {
    "textbody": "Lots of text.",
    "filetitle": "8-27.TXT.rtf",
    "entities": [
      {
        "name": "Mario Sartori",
        "type": "Person"
      },
      {
        "name": "Linda Grey",
        "type": "Person"
      },
      {
        "name": "Julia",
        "type": "Person"
      }
    ]
  },
  "8-28.TXT.rtf": {
    "textbody": "Also lots of text.",
    "filetitle": "8-28.TXT.rtf",
    "entities": [
      {
        "name": "Maine Health Foundation",
        "type": "Organization"
      },
      {
        "name": "Grzesiak",
        "type": "Person"
      },
      {
        "name": "Jim Williams",
        "type": "Person"
      }
    ]
  }
}

This is how I initialized Vue:

var vm = new Vue({
   el: '#all',
   data: {
       files: fullFileList
   }
})

Here's my HTML code:

  <ul id="all" class="nav flex-column nav-pills">
      <li v-for="(value,key) in files" >
        <a class="nav-link" id="messages-tab" data-toggle="tab" role="tab" aria-controls="messages" aria-selected="false">
        {{ key }} </a>
      </li>
    </ul>

However, no list elements are displaying. What could be causing this issue?

EDIT: To provide more clarity, here is how data is added to the fullFileList object variable:

The initial variable is defined at the beginning of the JavaScript file like so:

fullFileList = {}

New keys and values are added as follows:

  basefilename = path.basename(fileNames[loadFile])

  fullFileList[basefilename] = {}
  fullFileList[basefilename]['textbody'] =  result['html']
  fullFileList[basefilename]['filetitle'] = basefilename

Answer №1

In order to properly assign your object, ensure that you include the assignment to the object let fullFileList = .... Once this task is completed, the list of files will be displayed.

let fullFileList = {
  "8-27.TXT.rtf": {
    "textbody": "Lots of text.",
    "filetitle": "8-27.TXT.rtf",
    "entities": [{
        "name": "Mario Sartori",
        "type": "Person"
      },
      {
        "name": "Linda Grey",
        "type": "Person"
      },
      {
        "name": "Julia",
        "type": "Person"
      }
    ]
  },
  "8-28.TXT.rtf": {
    "textbody": "Also lots of text.",
    "filetitle": "8-28.TXT.rtf",
    "entities": [{
        "name": "Maine Health Foundation",
        "type": "Organization"
      },
      {
        "name": "Grzesiak",
        "type": "Person"
      },
      {
        "name": "Jim Williams",
        "type": "Person"
      }
    ]
  }
}

var vm = new Vue({
  el: '#all',
  data: {
    files: fullFileList
  }
})
.as-console-wrapper {display: none !important}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<ul id="all" class="nav flex-column nav-pills">
  <li v-for="(value,key) in files">
    <a class="nav-link" id="messages-tab" data-toggle="tab" role="tab" aria-controls="messages" aria-selected="false">
        {{ key }} </a>
  </li>
</ul>

Answer №2

An issue seems to have arisen with the code snippet

fullFileList{} instead of fullFileList = {}

One potential solution is to convert fullFileList into an array.

    fullFileList = [
      {"8-27.TXT.rtf": {
        "textbody": "A plethora of text.",
        "filetitle": "8-27.TXT.rtf",
        "entities": [
          {
            "name": "Mario Sartori",
            "type": "Person"
          },
          {
            "name": "Linda Grey",
            "type": "Person"
          },
          {
            "name": "Julia",
            "type": "Person"
          }
        ]
      }
},
      {"8-28.TXT.rtf": {
        "textbody": "Also a multitude of text.",
        "filetitle": "8-28.TXT.rtf",
        "entities": [
          {
            "name": "Maine Health Foundation",
            "type": "Organization"
          },
          {
            "name": "Grzesiak",
            "type": "Person"
          },
          {
            "name": "Jim Williams",
            "type": "Person"
          }
        ]
      }
}
    ]

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

Transforming Exposed components (jQuery Tools)

I need to toggle between two elements, exposing one while hiding the other with a single onClick event. Can this be achieved? My current JavaScript code does not seem to work as intended. Here is my existing script: function tutStep1(){ jQuery(' ...

Enhancing Angular Directives with Dynamic Templates upon Data Loading

I am facing an issue with a directive that is receiving data from an api call. While the directive itself functions properly, the problem seems to be occurring because the directive loads before the api call is complete. As a result, instead of the expecte ...

Ensure that Angular resolver holds off until all images are loaded

Is there a way to make the resolver wait for images from the API before displaying the page in Angular? Currently, it displays the page first and then attempts to retrieve the post images. @Injectable() export class DataResolverService implements Resolv ...

Can VueJS support multiple v-slots in a component?

I recently set up vee-validate v3.0 for validation in my project and everything was going smoothly until I tried to style my elements. Despite following the documentation on styling and making changes to the vee-validate config, I encountered a new issue - ...

What are the top methods for interacting between Server APIs and Client-Side JavaScript?

Presently, I am utilizing setTimeout() to pause a for loop on a vast list in order to apply some styling to the page. For example, For example: How I utilize setTimeOut: I use setTimeout() to add images, text and css progress bars (Why doesn't Prog ...

Tips for selecting an <select> option value based on an URL parameter automatically

I have a 2-step form where I am successfully passing the first name in the URL from step 1 to step 2, but I am struggling to do the same for a select field. Here's an example of what I have: In the URL: ?firstname=Bob Form Field: <input type= ...

Running TestCafe in headless mode with Chrome is sometimes causing inconsistent failures when using Vue event emit functionality

Browser: Chrome Version 91.0.4472.114 (Official Build) (x86_64) Testing Framework: TestCafe 1.14.2 Vue Code Snippets Template Example <input id="amount" v-model="amount" min="0" name="amount" class=&qu ...

Utilize jQuery to dynamically apply Bootstrap classes while scrolling through a webpage

Having an issue with jQuery. Trying to include the fixed-top class (Bootstrap 4) when scrolling, but it's not working as expected. jQuery(document).ready(function($){ var scroll = $(window).scrollTop(); if (scroll >= 500) { $(".robig").a ...

Exploring a nested JSON structure using AngularJS's Ng-Repeat directive

I am facing an issue with displaying all elements of subcategory using Ng-Repeat. I have managed to show all events from the selected category with this code, but I am unsure how to display all activities from a specific event. I currently have this code.. ...

Cursor not appearing when using router-link in VuetifyJS

<router-link to="/" tag="span" style="{ cursor: pointer; }">Name</router-link> However, the specified style is not being applied and the cursor remains as a text cursor when the mouse hovers over this element. Here is the full code snippet: ...

Utilize Bootstrap button dropdown to automatically assign a selected value to a list item

I have a form with a select box that transforms into a bootstrap button after the page loads. However, I am unable to set the selected value for the converted bootstrap button drop-down li. <button type="button" class="btn dropdown-toggle btn-default" ...

utilize text offsets to insert tags within strings

I've been facing challenges with using JavaScript string methods and regular expressions. It seems like I might be missing something obvious here. Please forgive me if I'm repeating the question asked by tofutim in more detail, which can be found ...

How to Utilize Output() and EventEmitter() for Value Transmission in Angular Application

Last week I was successfully able to implement Output() and EventEmitter() in my Angular app. However, today I am facing a new challenge while trying to apply the same concept in a different scenario. I'm not sure what I might be overlooking. Firstly ...

Setting up a textarea tooltip using highlighter.js

I'm experimenting with using highlighter.js within a textarea. I've customized their sample by substituting the p with a textarea enclosed in a pre tag (for right-to-left language settings). <div class="article" style="width: 80%; height: 80% ...

CoffeeScript's alert feature appears to be malfunctioning

After stumbling upon CoffeeScript in a blog post, I was excited to try it out. My first attempt at using it involved this code snippet: alert "Hello CoffeeScript!" Unfortunately, it didn't work as expected and produced the following error message: ...

Enhancing Material-UI component [TreeView] with drag and drop functionality using react-dnd

I have encountered an issue with my TreeView implementation. Whenever I try to drag and drop a TreeItem, all of its child TreeItems move along with it. Is there a way to enable drag'n'drop functionality for just one TreeItem without affecting its ...

AngularJS causing a modal popup to appear even when the associated button is disabled

When using a Bootstrap modal popup form that opens on button click in AngularJS, I noticed that the modal still appears even when the button is disabled. Can someone help me understand why this happens? Here is the code for the button: <a class="btn b ...

Numerous social media sharing buttons conveniently located on a single webpage

Currently, I am working on a research database project where the goal is to allow users to share articles from the site on various social networks such as Facebook, Twitter, LinkedIn, and Google+. To achieve this, I successfully implemented share buttons ...

Guide to implementing a DataTable in MVC4 UI using jQuery

Looking to set up a DataTable using jQuery similar to the one shown in this image: https://i.stack.imgur.com/DNUcd.png I'm not very comfortable with jQuery, so please be patient and avoid asking me what I've tried. I don't know how to stru ...

Screen readers are unable to "read" text that is identified through aria-describedby

When enhancing my web application for accessibility, I encountered a challenge. I have implemented two required fields in a form that should display separate error messages as spans. To accomplish this, I am utilizing aria-invalid to signal when the fields ...