Displaying array data without the need for a loop in Vue.js and Axios

I want to display data in my Vue.js 3 app without using a loop. Here is the response from my Axios API: In My Axios Api I got reponse:

[{id: 2, name: "sub_title", value: "The Best Developer Team", created_at: null, updated_at: null},…]
0
: 
{id: 2, name: "sub_title", value: "The Best Developer Team", created_at: null, updated_at: null}
1
: 
{id: 3, name: "title",…}
2
: 
{id: 4, name: "description",…}
3
: 
{id: 5, name: "button_text", value: "Get Started", created_at: null, updated_at: null}
4
: 
{id: 6, name: "video_text", value: "How We Works", created_at: null, updated_at: null}
5
: 
{id: 7, name: "video_url", value: "#video", created_at: null, updated_at: null}
6
: 
{id: 8, name: "url", value: "/frontend/assets/img/hero/1667138531.png", created_at: null,…}
7
: 
{id: 9, name: "image", value: "1667138531.png", created_at: null, updated_at: null}

And Vuejs Component:

axios.get('/api/sites/hero').then(response => {

                    this.heros = response.data.map(obj => {
                        let hero_data = {};
                        hero_data[obj.name] = obj.value;
                        console.log(hero_data);
                        return hero_data;
                    })
 
                }).catch(function (handleError) {
                    console.log("Error: ", handleError);
                }, []);

And my console log displays:

{sub_title: 'The Best Developer Team'}
{title: 'We Are the Brilliants Creative Secure Maintain Build in Terms of website Development'}
{description: 'End-to-end payments and website management in a si…le solution. Meet the right team to help realize.'}
{button_text: 'Get Started'}
{video_text: 'How We Works'}
{video_url: '#video'}
{url: '/frontend/assets/img/hero/1667138531.png'}
{image: '1667138531.png'}

Now I am trying to print {{heros.title}} in my div, but it's not working.

Answer №1

It's more efficient to have a single dictionary.

axios.get('/api/sites/hero').then(response => {

                    this.heros = {}
                    let self = this;
                    response.data.map(obj => {
                        self.heros[obj.name] = obj.value;
                        return obj;
                    })
 
                }).catch(function (handleError) {
                    console.log("Error: ", handleError);
                }, []);


{sub_title: 'The Best Developer Team', title: 'We Are the Brilliants Creative Secure Maintain Build in Terms of website Development', description: 'End-to-end payments and website management in a si…le solution. Meet the right team to help realize.', button_text: 'Get Started', video_text: 'How We Works', video_url: '#video', url: '/frontend/assets/img/hero/1667138531.png', image: '1667138531.png'}

This will create a dictionary structure like the one above, allowing you to access elements directly.

heros.title

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

Can this pagination task be accomplished without the use of backgrid?

I have been exploring ways to implement server-side pagination similar to what Datatables offers, and during my search I came across the backbone.paginator library on GitHub. However, I am curious if there are any other options available as well. After ex ...

My React app experienced a severe crash when trying to render an animation in L

Currently, I am working on a React application that was set up using Vite. I recently incorporated an animation using Lottie, and although I was successful in implementing it, I encountered a problem when navigating between different pages of my applicati ...

What is the best way to update text in an element when hovering or activating it?

I am currently designing a website with a prominently placed "Hire me" button in the center of the page. The button was implemented using the following code: <div id="hire_me_button"> <a href="contact.html"><h4 id="hire_me" class="butto ...

Record the clicks registered within an external jQuery UI modal window

I have a jQuery UI dialog window that contains thumbnail images. When an image is clicked, I want to retrieve the id of that specific image. I believe using data attributes can help me achieve this easily. However, I'm facing an issue where I am unab ...

The Node.js Express Server runs perfectly on my own machine, but encounters issues when deployed to another server

I've encountered a strange issue. My node.js server runs perfectly fine on my local machine, but when I SSH into a digital ocean server and try to run it there, I get this error. I used flightplan to transfer the files to the new machine. deploy@myse ...

Access and retrieve pkpass files from a server using React

I've exhausted all options but still can't get it to work. I'm attempting to create an Apple wallet pass using https://github.com/walletpass/pass-js. When I download the pass on the node server where I've implemented it, everything work ...

Add different input strings to an array within the scope when there is a change in the input value (AngularJS)

My goal is to populate an empty array within the scope with strings. The number of elements in this array can vary depending on what the user types in the player count input field. $scope.playerNames = []; To achieve this, I am using a $watch function th ...

Learn how to showcase vertical text in React Native similar to a drawer

Looking for a way to display text vertically in a dynamic manner, where the length of the text can vary. Below are some examples for reference: Example 1 Example 2 <View> <View style={{}}> <View style={{ marginTop: 30, flexDire ...

Django compressor throwing minification hiccup

I'm currently utilizing a tool for automatic minification known as django compressor. Unfortunately, it seems that the minification process with django compressor is causing errors to be introduced. Updated script with semicolons: Before: var ap ...

encoding the special character "ü" in json_encode as either 'ü' or '&#

I've developed a function that extracts the title from a given URL and returns it in JSON format. The function is invoked by an AJAX call. Everything works smoothly, but when a title contains characters like ü or any related ones, it returns null. Wh ...

How can I create an efficient chat system using Ajax and settimeout without causing excessive virtual memory usage?

I'm in the process of creating a chat application using AJAX that fetches data every second with setTimeout. I have drafted a basic code where there is a number that increments each second by the number retrieved from the PHP page2. Upon testing it on ...

Currently, I am developing a customized stylesheet specifically designed for Internet Explorer versions 10 and 11

Is it possible to utilize this straightforward script for identifying IE versions 10 and 11? if($.browser.version == 11.0 || $.browser.version == 10.0) { $("body").addClass("ie"); } ...

Integrate a scrollbar seamlessly while maintaining the website's responsiveness

I encountered an issue where I couldn't add a scrollbar while maintaining a responsive page layout. In order to include a scrollbar in my datatables, I found the code snippet: "scrollY": "200px" However, this code sets the table size to 200 pixels r ...

Issue encountered while configuring 'innerHTML' in xmlHttp.onreadystatechange function

Trying to create a JavaScript function that changes the innerHTML of a paragraph within an xmlHttp.onreadystatechange function, I encountered an error in the Chrome Console: Uncaught TypeError: Cannot set property 'innerHTML' of null at XMLH ...

Error message indicating that the function is not defined within a custom class method

I successfully transformed an array of type A into an object with instances of the Person class. However, I'm facing an issue where I can't invoke methods of the Person class using the transformed array. Despite all console.log checks showing tha ...

Error: There was a syntax issue when trying to parse JSON due to an unexpected identifier "object" in the anonymous function

I'm having trouble understanding why there was an issue parsing this file: { "t": -9.30, "p": 728.11, "h": 87.10 } This is the javascript code I used: <script type="text/javascript"> function verify() { $.get("http://....file.json", funct ...

Do not allow the use of <p> or <br> tags in

I would like to enable basic styling with <strong>, <em> and lists using ckeditor. But I do not want any <br> or paragraph tags because I normalize my content and prefer it to be clean. After some research online, I came across this sol ...

Using Tinymce to automatically send form on blur action

Attempting to trigger form submission upon blur event in Tinymce, but encountering difficulties. tinymce.init({ selector: 'textarea.html', menubar:false, force_br_newlines : true, force_p_newlines ...

Adjust the position of the content to the bottom for a form-group in Bootstrap v4

Currently, I am facing an issue where my label is overflowing and wrapping around the input field, causing it not to align at the bottom of the div. Is there a straightforward CSS solution for this problem? I have experimented with position relative/absol ...

How to eliminate unnecessary space when the expansion panel is opened in material-ui

Is there a way to eliminate the additional space that appears when clicking on the second accordion in material UI? When I open the second accordion, it shifts downward, leaving a gap between it and the first accordion. Any suggestions on how to remove thi ...