What is the best way to showcase JSON keys in Vue.js?

I have a JSON structure that resembles the following:

    {
    "data": [
        {
            "name": "someName",
            "link": "http://somelink.com",
            ],
            "relevantArray": [
                {
                    "keyIWant": 42
                }
            ],
            ...
     }

When accessing the data in my Vue component, I use the following code:

<li v-for="(dataVue, index) in dataList.feat" :key="index">
          {{ dataVue.someName }}
          {{ dataVue.link}}
          {{ dataVue.??? }}
</li>

How can I access just the name of "keyIWant" - the key, not the value (42)?

UPDATE: I am able to display the relevant JSON by using

{{dataVue.relevantArray}}

However, this outputs the JSON in the format:

[ { "relevantArray": keyIwant } ]

and I am struggling to return just the relevantArray on its own. Check out the CodeSandbox example for more details:

https://codesandbox.io/s/festive-wu-41kyz?file=/src/App.vue

Answer №1

Check out this helpful code snippet: https://codesandbox.io/s/mystifying-brook-42m3x

Essentially, with the v-for directive, you can iterate through any object as key-value pairs.

<li v-for="(vueData, index) in vueData.data" :key="index">
  {{ vueData.someName }}
  {{ vueData.link }}
   <span
     v-for="(r,ri) in vueData.relevantArray[0]"
     :key="ri"
     >{{ ri }} ==> {{ r }}
   </span>
</li>

This piece of code will display output like:

someName http://somelink.com keyIWant ==> 42

Answer №2

Although Arief's response didn't resolve the issue, I decided to tinker with it and ultimately discovered my own solution:

<span
     v-for="(r,ri) in vueData.relevantArray[0]" :key="ri">
      {{ ri }} ==> {{ r }}
         <span v-for="(arrayData, index) in r" :key="index">
            {{index}}
         </span>
   </span>

The above code successfully produced the desired output for me. Despite this, I will mark Arief's suggestion as the accepted answer since it ultimately led me to my solution.

If anyone has insights into why my version was necessary, I would greatly appreciate the explanation.

Many thanks to everyone who contributed!!

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

Automatically adjusting the height of an iFrame using jQuery

Is there a way to automatically adjust the height of my iframe based on its content? I've successfully achieved this using a jQuery plugin called jquery-iframe-auto-height by house9. Now, I want the homepage within the iframe to fill the entire heigh ...

Tips for efficiently storing a large JSON object in a session storage?

Currently, I am working on a project that involves saving rows retrieved from a database as JSON data to the user's session. This application utilizes jQuery on the client-side and is powered by the Spring MVC framework along with Hibernate on the bac ...

Attempting to resolve the Bootstrap issue, currently in search of a suitable solution

Today I made the decision to utilize bootstrap to create a menu, but unfortunately, I've encountered a strange bug or conflict involving jQuery, potentially the UI, and Bootstrap. If you'd like, you can take a look at the picture provided in the ...

How can I implement a division animation with Javascript upon clicking a hyperlink?

I need help with animating a div when clicking on a link. Specifically, when clicking on the "About" link, the height of the div should change. Currently, the height is set to '0em' in the CSS file. I attempted to change the height using "documen ...

Creating tailored pages in Nuxt.js is key for a successful website

Is there a way to selectively generate specific pages from a Nuxt project, rather than building all pages at once? For instance, if we have the following structure in our pages directory: pages/ | |- index.vue |- about-us.vue |- pricing.vue |- moduleA ...

What is the optimal way to develop an npm module with a predefined name?

I am currently developing a module in nodejs and utilizing NAPI for its creation. Let's assume the name of my module is "hello", so it would be required in JavaScript as require("hello") However, I wish to require it like this require("Name/hello") ...

Sending form input values to JavaScript

Struggling to design a webpage featuring one text box and a button that, when clicked, sends the value to Javascript for further processing? After spending significant time troubleshooting, I'm unsure of what's causing the issue. Below are both t ...

Conceal all table rows with the exception of the row that has been clicked

I am working on a table that uses ng-repeat to populate its rows. When a row is clicked, I am able to retrieve data related to the object using ng-click. The table gets populated with information from a JSON file. My question is, how can I make it so tha ...

Apply a see-through overlay onto the YouTube player and prevent the use of the right-click function

.wrapper-noaction { position: absolute; margin-top: -558px; width: 100%; height: 100%; border: 1px solid red; } .video-stat { width: 94%; margin: 0 auto; } .player-control { background: rgba(0, 0, 0, 0.8); border: 1px ...

Once the submission is made, the website will automatically refresh

As I delved into working on a web page utilizing JavaScript, I implemented a jQuery function to validate user input data. I ensured that if the data was incorrect, the function would return false (return false;) and the form would not refresh the page. How ...

Retrieve a distinct property of an element from an array of objects fetched from an API using Vue 2

I successfully retrieved an array of objects from an API in mounted within Vue. The array consists of various objects with properties such as author, genre, poster, title, and year. 0: author: (...) genre: ("Rock") poster: (...) title: (...) year: (...) 1 ...

Retrieve the input field's value with Selenium, verify its accuracy, and proceed to log a message to the console

Hey there! I'm facing a challenge while working with Selenium Webdriver, specifically Chrome Webdriver and writing tests in JavaScript. The problem is in a section of the code where I can't seem to grab the value typed into an input field using t ...

Looking to set a cursor style on a table row with JavaScript?

let table = document.getElementById(TABLE_NAME); let nextRow = table.tBodies[0].rows.length; row.setAttribute('style', "cursor: pointer;"); I am trying to implement a double click event on a table row, which is working as expected in most ...

Empty area beneath the body in Chrome Device Mode

Can anyone explain why there is a strange blank space under the body tag in mobile view on Chrome (other browsers like Firefox and Internet Explorer display it correctly)? Here is the simple code that I am using: <html> <meta name="viewport" con ...

Creating an HTML form to collect user data and then automatically saving it to an Excel file stored in a shared Dropbox account

I am looking to extract data from a user form on a website and then automatically save it as an Excel file in a designated Dropbox account once the user clicks submit. Instead of receiving multiple emails every time the form is filled out, I would like t ...

How to utilize a parameter value as the name of an array in Jquery

I am encountering an issue with the following lines of code: $(document).ready(function () { getJsonDataToSelect("ajax/json/assi.hotel.json", '#assi_hotel', 'hotelName'); }); function getJsonDataToSelect(url, id, key){ ...

Is there a restriction on the number of strings allowed in minimist?

Here is the output received from the code provided below. Question input and i are both true as intended, but why aren't project and p? They are defined in exactly the same way as input and i. $ bin/test --input -p { _: [], update: fa ...

Encountering a frustrating internal server error 500 when attempting to insert values from JavaScript into SQL Server within Visual Studio

Here is a JavaScript code that calls a C# webmethod: var _data = { '_mStart': document.getElementById("St_Period").value, '_mEnd': document.getElementById("En_Period").value }; $.ajax({ type: "POST", url: "maps.aspx/my ...

Ways to organize extracted JavaScript information from objects based on a nested object attribute

I am attempting to arrange objects numerically in an array in ascending order, using a nested object property called 'score'. Here is the Array: [ {name:'dan', score:220}, {name:'lucy', score:876}, {name:'mike' ...

NodeJS instructs open(html) to execute first before any other code is executed

I am evaluating whether nodeJS is a suitable solution for a project I am working on during my internship. To summarize: I need the basicNode.js file to wait until the open('./basic.html') command has completely opened the browser and loaded its c ...