Exploring VueJS: How to retrieve a specific value from an array within JSON data obtained from an API request?

Currently, I am creating a blog completely from scratch with both front and backend components that communicate through an API I developed. Below is the JSON output of the API:

{
    "status": true,
    "posts": [
        {
            "id": 2,
            "title": "a la zeub",
            "description": "zebi",
            "user_id": 1,
            "created_at": "2022-12-08T12:16:58.000000Z",
            "updated_at": "2022-12-08T12:16:58.000000Z"
        },
        {
            "id": 4,
            "title": "title2",
            "description": "thing",
            "user_id": 1,
            "created_at": "2022-12-08T12:39:15.000000Z",
            "updated_at": "2022-12-08T12:39:15.000000Z"
         }
    ]
}

For my frontend, I utilize VueJS. When I need to display a specific value from the API response, I use the following syntax in the HTML section:

<p> {{ the name of the variable }} </p>

However, I am facing an issue where if I attempt to display posts.status using this method, it correctly shows "true" in the p tag. Yet, when trying something like posts.posts.id or posts.posts['id'], it does not work as expected. The p tag remains empty in the source code. What should I input within the brackets to access the title?

Answer №1

Agree with @LawrenceCherone and following up on the comments, it is important to implement something along these lines-

To illustrate, let's take a variable posts within the data property and assign your JSON result to it.

<html>
<div id="app">
  <p v-for="(post, index) in posts.posts" :key="index">{{ post.title }}</p>
</div>

<!-- Make sure to add Vue from CDN! -->
<script src="https://unpkg.com/vue@2"></script>
<script>
  new Vue({
    el: '#app', //Instructs Vue to render in HTML element with id "app"
    data() {
      return {
        posts: {
         status: true,
         posts: [
          {
            "id": 2,
            "title": "a la zeub",
            "description": "zebi",
            "user_id": 1,
            "created_at": "2022-12-08T12:16:58.000000Z",
            "updated_at": "2022-12-08T12:16:58.000000Z"
           },
           {
            "id": 4,
            "title": "title2",
            "description": "thing",
            "user_id": 1,
            "created_at": "2022-12-08T12:39:15.000000Z",
            "updated_at": "2022-12-08T12:39:15.000000Z"
            }
         ]
        }
      }
    }
  });
</script>
</html>

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

CSS Attribute Selector Wildcard Capture Tool

Suppose I have the following HTML structure: <div class="tocolor-red"> tocolor </div> <div class="tocolor-blue"> tocolor </div> <div class="tocolor-green"> tocolor </div> <div class="tocolor-yellow"> tocolor ...

VueJS component experiencing stagnant DOM updates

I have reviewed similar posts on "DOM not updating", but have yet to find a solution. I am working on a task app and it can successfully load, add, and delete tasks from Firestore. However, I'm facing two issues and would like to address the first one ...

What methods can be used to test the functionality of bootstrap-vue toasts?

In order to confirm that our mistakes have been displayed in the bootstrap-vue toast messages, I need guidance on how to validate this during testing. My initial plan was to simulate 'this.$bvToast.toast', but so far I haven't found a suitab ...

"Extracting regular expressions between the penultimate and ultimate characters

I'm struggling with a simple regex question. I want to extract text between two specific characters: - and ~ Here's my string: Champions tour - To Win1 - To Win2 ~JIM FURYK Currently, when I use this regex pattern: \-([^)]+\~), it mat ...

Using Jquery to Iterate Over Data

Is there a way to iterate through the JSON data string below using jQuery? Would it be appropriate to use $.getJSON function for this task? jQuery17209521235961001366_1380903443191({"type":"result","rid":"hopkinsa","data": [[{"artist":"NAME1","title":"SON ...

The dominance of the parent component's scoped style over the child component's scoped style

Within my Vue application, I have two components. The main component utilizes a class named elDiv, which results in a yellow background color. On the other hand, the secondary component also employs a class named elDiv, but with a blue background color. De ...

CKEditor directive in AngularJS does not properly enforce the maxlength attribute in textarea

I am currently working on an AngularJS application with the CKEditor plugin. I have created a directive for CKEditor and everything seems to be functioning properly. However, I am facing an issue where I need to limit the character length to 50. I tried us ...

Looking for assistance with implementing a jQuery function for an onClick event on

I've been diving into learning jquery and managed to create a basic checkbox feature with a function that allows you to set all options as read-only by checking the "None of the above" button. <html> <body> <form id="diagnos ...

"Learn the steps to toggle a sub menu using an onclick event and how to hide it using another

I created a sidebar navigation that displays submenus on mouseover, but I want them to open on click and close when clicking on the same tab. Please take a look at my code on this CodePen link. Thank you. <nav class="navigation"> <ul class="mai ...

Protecting a NodeJS Backend Application with Secure POST Request Handling

I need to set up a way to accept POST requests from a backend java application containing user account details (such as username, email, etc.) in order to create accounts based on that information. How can I secure the API so that it can only be accessed ...

Having trouble navigating through multiple layers of nested array data in react js

I need help understanding how to efficiently map multiple nested arrays of data in a React component and then display them in a table. The table should present the following details from each collection: title, location, description, and keywords. Below ...

JavaScript / Ajax / PHP - A Minor Bug That's Bugging Me

I'm in need of some help, as I feel like I'm losing my mind right now. I've developed a code using php/js/ajax to verify if an email address exists when it's submitted. After testing the ajax, I can confirm that the correct values are ...

Visualizing Data with d3.js Force Chart: Integrating Images with Nodes for Dynamic Animation

After enhancing a force diagram to compare two profiles, I am faced with the challenge of getting the main node to display an image. View comparison here How can I centrally align and size the image correctly while making the thumbnail data from the JSO ...

Ways to retrieve output from a JavaScript function

How can I successfully return the result from response.on within the signIn function? const signIn = async (password) => { var request = new DeviceAuthQuery(); request.setPassword(password); var response = client.authenticate(request, {}, (err, ...

Retrieve the inner content of parentheses within a string, utilizing recursion for nested parentheses

I am currently working on a function that will extract words enclosed in parentheses and store them in their own array, accounting for nested parentheses recursively. For example, when given the string "((a b) ugh (one two)) pi", I would like it to be tra ...

Slow loading time when switching between items in the drop-down menu from the Main Menu

Visiting my website at europebathroom.com, you will notice a horizontal main menu with dropdown functionality. When hovering over a menu item, the corresponding dropdown appears seamlessly. Yet, sometimes quickly touching another menu item unintentionally ...

Updating a Vuetify 3 Theme on the Fly

I am looking to dynamically switch themes. I have defined a lightTheme and darkTheme. export default createVuetify({ theme: { defaultTheme: "lightTheme", themes: { lightTheme: { dark: false, colors: { pri ...

Combining text output using JavaScript, HTML, and Vue

Can you help solve this challenge of outputting concatenated text from a javascript code? The script in question draws a quarter circle that is proportional to the size of the bar and showcases the value of pi accurate to three decimal places. To display ...

Struggling to determine if the checkbox has been ticked?

I have integrated a like button on my website using socket io to ensure that it updates for all users when the like button is clicked. I have successfully implemented a like() function that emits liked, increments a counter, and displays it on the page. Ho ...

Tips for making multiple successful calls to Firebase

Situation: I am tasked with retrieving file download URLs, given an array of file names. To achieve this, I make multiple calls to Firebase storage in order to obtain the file URLs and create a new array containing these URLs. Approach Used: I iterate t ...