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

How can I ensure that my user responses are case-insensitive?

Currently, I'm facing a roadblock in my coding journey. I am trying to modify my code so that user responses are not case sensitive when compared for correctness. Can anyone offer some advice on how to achieve this? Check out the code snippet below: ...

No data was returned in the responseText of the XMLHttpRequest

I am facing an issue where my XMLHttpRequest code is executing without any errors, but it always returns an empty responseText. Here is the JavaScript code that I am using: var apiUrl = "http://api.xxx.com/rates/csv/rates.txt"; var request = new XMLH ...

The Harmony of FeathersJS and VueJS 2: A Collaboration in Code

Looking for a simple solution to set up scaffolded versions of both feathers and vue without conflicts, utilizing their respective -cli tools. The goal is to deploy both simultaneously with the following requirements: Avoid triggering recompilation in fe ...

Using Javascript to choose an option from a dropdown menu

const salesRepSelect = document.querySelector('select[name^="salesrep"]'); for (let option of salesRepSelect.options) { if (option.value === 'Bruce Jones') { option.selected = true; break; } } Can someone please ...

Is randomly pairing 2 datapairs after slicing a JSON array easy or challenging?

There is a JSON file containing an unlimited number of users [{ "fname": "Hubert", "lname": "Maier", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bd ...

Interactive section for user input

I am looking to add a commenting feature to my website that allows for dynamic editing. Essentially, I want users to be able to click on an "Edit" span next to a comment and have it transform into an editable textarea. Once the user makes their changes and ...

Samsung Galaxy S7 can interpret alphabetical parameters as numbers in a link sent via SMS

When trying to open a text message with a new message on my website using a link, I encountered an issue specifically with the Galaxy S7. The following code works on most Android phones: sms:5555555555?body=JOIN However, on the Galaxy S7, the "?body=JOIN ...

Probability of an event occurring when represented as whole numbers in percentage form

Currently, I'm developing a unique job system within a Discord bot that allows users to mine various types of ores. The probability of receiving specific ores is based on the user's mining skill level, which is stored in a database and can vary a ...

What is the best way to retrieve the page slug within the layout file in Next.js, specifically when using the app folder structure?

In my latest project, I have implemented a nested layout using the new app folder. Within this layout, I have included a header that appears across all pages in the path www.mysite.com/some-slug. One issue I am facing is with the signup button located in t ...

Tips for preserving line breaks when sending a message through the mail

Hi, I'm currently facing an issue: I am trying to send text from a textarea using POST to a PHP script that will write it to a file and display it on the website. However, when I do this, the line breaks disappear and the displayed text ends up lookin ...

Picture disappearing following the refresh of an AJAX iframe

Currently, I am developing a profile system where the user's profile is displayed in an iframe that reloads when the form submit button is clicked. The content updates successfully upon reloading, but there is an issue with images not displaying after ...

Is there a way to convert HTML into a structured DOM tree while considering its original source location?

I am currently developing a user script that is designed to operate on https://example.net. This script executes fetch requests for HTML documents from https://example.com, with the intention of parsing them into HTML DOM trees. The challenge I face arise ...

The server is unable to process the .get() request for the file rendering

Struggling with basic Express routing here. I want the server to render 'layout2.hbs' when accessing '/1', but all I'm getting is a 304 in the console. Here's the error message: GET / 304 30.902 ms - - GET /style.css 304 3.3 ...

How is it possible to retrieve the flex-direction property value of a div using JavaScript?

While attempting to troubleshoot this issue using Windows Chrome 59, I utilized devtools to examine the properties. let element = document.getElementById("divId"); Upon checking the value of the element, I noticed that the object structure displayed: &g ...

Using the React key attribute for components without distinct identifiers

When dealing with a situation where users need to provide a list of timeframes, it can be tricky to generate a unique key for each component in React. Simply using the index of the array is not sufficient, as items can be removed from the middle of the lis ...

What could be the reason behind the login button not triggering the console message display?

I've decided to delve into web server development on my own and have been tweaking a GitHub repository for ExpressJS with Typescript that I stumbled upon. My initial goal is simple - just to have something displayed on the console when I click the log ...

Vue is lagging behind in implementing Virtual Dom technology

I have come across this code snippet. <template> <div ref="nodesData"> <div v-for="(item, index) in nodes" :key="index" :id="item.id"> {{ item.id }} </div> <div> ...

Include an external JavaScript file within a component to display pictures in a web browser using Angular 2

I am developing a website using Angular 2 and need to incorporate a JavaScript file into a component. The goal is for this script to adjust the height of certain images to match the height of the browser window. What is the best way to integrate this scri ...

Issue with redirect using Node.js promise

I’ve created a settings page where users can add and remove filters. To handle the deletion process, I’ve implemented this jQuery function: $('#delete-filter').click(function (e) { var filtername = $('#filter-list').val(); ...

The expiry date of the cookie remains unchanged

When attempting to create a cookie and specify an expiration date, I am encountering an issue where it remains as "Session". This problem is occurring in Google Chrome. I'm looking for insights on what might be causing this behavior. The code snippe ...