How to retrieve an object within an object using a dynamic key in Vue JS?

Within my Vue JS 2.x project, I am utilizing a v-for loop to iterate over an array of objects. Each object in the array contains a dynamically named "key", which is unique to each object. My goal is to access the data associated with each key in order to display information within the v-for loop. Here is a snippet of my array data structure...

[{
  "6457": {
    "agent": {
      "id": 4003,
      "memFree": 0
    }
  }
}, {
  "7809": {
    "agent": {
      "id": 7809,
      "memFree": 20
    }
  }
}]

I attempted to access the key using [0], but unfortunately, that did not yield any results in this scenario.

<div v-for="(server, index) in servers" :key="index">
  <!-- Outputs a single object based on the key -->
  {{ server }}

  <!-- Does not output the desired result -->
  {{ server.memFree }}

  <!-- Also does not output the desired result -->
  {{ server[0].memFree }}
</div>

I am puzzled by what I might be overlooking in this setup. Any guidance would be greatly appreciated.

Answer №1

<div v-for="(host, i) in hosts" :key="i">
  {{ Object.values(host)[0].device.diskFree }}
</div>

Answer №2

Make a change to your template like so

<div v-for="(server, index) in servers" :key="index">
  <div v-for="(agentData, property) in server" :key="property">
    {{ agentData.agent.memFree }}
  </div>
</div>

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

The onchange function seems to be malfunctioning when attempted with AJAX

Help needed: I'm new to AJAX and facing an issue. I have implemented AJAX pagination to display data, which is working fine. But now I want to add a filter to the displayed data, however, the filter is not functioning correctly. <select name="prof ...

Using RadSideDrawer with Typescript in Vue class components: A Step-by-Step Guide

I am attempting to integrate external components into Vue Typescript Class Components. Following the installation of the standard template, I made modifications to its <script> block based on this guide: import { Vue, Component, Prop } from "vue-pro ...

Using MongoDB to find a specific element in an array and then make updates

I have performed aggregation to calculate the total and the objects that contribute to the total. Now, I need to update the source table with the aggregated object ID for the elements involved in the aggregation, establishing a two-way relationship. col ...

Creating a sleek animation with JQuery for a dynamic-width div

Here is a snippet from my latest project. Take a look at the demonstrationFIDDLE. In this project, I have created a list with buttons: <a href="#f1" class="bt">1 <div class="show">Computers Networking</div> </a> When you ...

What is the best method for establishing environment variables across different platforms?

When working with Node scripts on Windows, the format should be like this: "scripts": { "start-docs": "SET NODE_ENV=development&&babel-node ./docs/Server.js" } However, on Linux, the SET command is not used, so it would look like this: "scri ...

Add an element to the parent body from an iframe with this easy tutorial!

Within the iframe, I have the following code snippet. <script type="text/javascript"> $(document).ready(function(){ $("body").append($("#ctap").html()); }); </script> I am looking to append the HTML content of #ctap to the par ...

Retrieve data from the page and component

Incorporated within my <Categories/> component are functionalities to interact with the main page using the fetch method to dispatch actions. The fetch functionality within the <Categories/> component operates smoothly as intended. A 're ...

Firebase Database: Unidentified node kind with separate Firebase initialization process

In order to replicate this issue, a minimal repository can be found at: https://github.com/ljrahn/firebase-unknown-node-type The problem arises when the firebase initialization logic is separated into a distinct package and then imported. This leads to an ...

I am looking for an alternative approach to retrieve the data from the controller in case of success instead of the method provided in the code below

In this code snippet, I am looking for an alternative way to retrieve data from the controller upon successful execution, rather than checking it as data.msg. I want to avoid using strings or boolean values like SuccessWithPass. Controller snippet: if ...

Struggling to customize checkbox design using HTML and CSS

Why am I unable to style HTML checkboxes with CSS? No matter what I try, the appearance of the checkboxes remains unchanged: Here is the HTML code: <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /> <nput type="checkbox" id ...

Utilizing Google Caja for JavaScript sanitization is the only way to ensure

Looking to safeguard the inputs provided to a nodejs server with the assistance of the google-caja sanitizer. However, it's somewhat overzealous and cleanses away the > and < symbols too. My project requires me to retain html characters in the ...

Is it possible to bind Tailwind classes with Storyblok?

Currently, I am in the process of creating components for our content editors to use in Storyblok. There is a particular scenario where we want to specify layout properties (using Tailwind's classes) through props received from Storyblok components. ...

Encountering an issue in Angular 8 where a class is not being added after the page loads, resulting in an

Looking to dynamically add a class to a div element using Angular? I have a condition isTrue, and am utilizing the angular function ngAfterViewInit() to add the class hideOnLoad after the page loads when isTrue becomes true. Instead of traditional javascri ...

Learning fundamental MVC concepts with Express.JS

Express.js offers a basic framework for implementing the standard MVC development pattern. However, many tutorials I have come across tend to mix controller logic within route files or a global app file. In an ideal scenario: Model - Manages core behavio ...

Node.js and Express make it easy to provide XLS download functionality

I have successfully utilized the code snippet below to generate an excel file in node.js. My intention is for this generated file to be downloadable automatically when a user clicks on a designated download button. var fs = require('fs'); var w ...

Adjust the attributes of v-data-table

I am working with a v-data-table and I need to create a method that can change the attribute "loading" to false within the method. Can anyone provide guidance on how to accomplish this? <v-layout fluid v-resize="onResize" child-flex> <v-da ...

Exploring the integration of query parameters in Postman using mongoose

In my code, I have a driver.js file that holds a driver schema, and a driverController.js file with REST methods including GET, POST, DELETE, and PUT. What I want to achieve is to send a GET request to http://localhost:3000/drivers?available=true This re ...

Using array map to create a centered table in a React web application

In my React project, I created a table using the array.map function. However, I'm facing an issue where the output of array.map is always aligned to the left, even before the table itself. I want to center the entire table. JSX: <div className=&qu ...

"Enhancing the speed of the JavaScript translate function when triggered by a scroll

I am facing an issue with setting transform: translateY(); based on the scroll event value. Essentially, when the scroll event is triggered, #moveme disappears. For a live demonstration, please check out this fiddle: https://jsfiddle.net/bo6e0wet/1/ Bel ...

Looking to install nodemon for Node.js on macOS? If you're encountering a "command not found" error, here's

After installing nodemon using the command npm install -g nodemon, I encountered a Permissions issue. To resolve this, I used the sudo npm install -g nodemon command. However, when attempting to run the "nodeman" command, I kept receiving an error that s ...