Show an item in a visual format of a list

Need help displaying a specific object:

cars: {
    number': 1, 'overload': 1,'brand': {'0': {'porsche': [{'price': 10, 'id': 0}],'vw': [{'price': 20, 'id': 1}]}}},

I am looking to convert it into a list format (see example for the first key):

Number : 1
Overload : 1
Brand:
Porsche: 
price: 10
id: 0
VW:
price: 20
id: 1

Any suggestions on how I can achieve this?

Answer №1

To achieve the desired display, you can utilize the code below in Vue. However, for greater flexibility, consider creating sub-components and passing items as props for enhanced control. This example simply illustrates the necessary steps to iterate through your data structure.

new Vue({
  el: '#app',
  data() {
    return {
      obj: {"0":{"color":[{"red":1,"value":100}],"brand":[{"id":0,"price":100}]},
            "1":{"color":[{"blue":1,"value":100}],"brand":[{"id":0,"price":100}]},
            "2":{"color":[{"green":1,"value":100}],"brand":[{"id":0,"price":100}]},
            "3":{"color":[{"yellow":1,"value":100}],"brand":[{"id":0,"price":100}]},
          },
    };
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <div v-for='(value,id) in obj' :key='id'>
    <div v-for='(rowvalue,rowid) in value' :key='rowid'>
      {{rowid}}
      <div v-for='(subvalue,subid) in rowvalue' :key='subid'>
        <div v-for='(listvalue,listid) in subvalue' :key='listid'>
          {{listid}} : {{listvalue}}
        </div>
      </div>
    </div>
  </div>
</div>

This will output:

color
red : 1
value : 100
brand
id : 0
price : 100
color
blue : 1
value : 100
brand
id : 0
price : 100
color
green : 1
value : 100
brand
id : 0
price : 100
color
yellow : 1
value : 100
brand
id : 0
price : 100

You can further customize the formatting based on your requirements. I hope this explanation assists you.

Answer №2

I believe the solution you are seeking is this:

data = {"0":{"color":[{"red":1,"value":100}],"brand":[{"id":0,"price":100}]},
 "1":{"color":[{"blue":1,"value":100}],"brand":[{"id":0,"price":100}]},
 "2":{"color":[{"green":1,"value":100}],"brand":[{"id":0,"price":100}]},
 "3":{"color":[{"yellow":1,"value":100}],"brand":    [{"id":0,"price":100}]}
}
Object.keys(data).forEach(function(key,index) {
  // key: "0", "1", "2", "3" in your scenario
  console.log(data[key])
});

You may also use

for (var key in data) {
   console.log(data[key])
}

This will provide you with the desired object containing "color" and "brand" properties.

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

Incorporating CASL with the latest version of Angular, version

I'm currently working on implementing CASL into my Angular application, but I'm having trouble understanding how to integrate it. // Login Component ngOnInit() { var jsonBody = {}; jsonBody['email'] = 'peter@klaven'; ...

Any tips for filtering an array within an array of objects using the filter method?

I have an array of products and models that I am currently filtering based on 'id' and 'category'. var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.products = [{ 'id': 1, ...

Tips for splitting the json_encode output in Javascript

Looking for help with extracting specific values from JSON data retrieved via PHP and AJAX. I only want to display the agent name in my ID, not the entire object that includes "name" : "Testing". Here is the console output: [{"agent_module_id":"1","agen ...

Personalizing the text of an item in a v-select interface

Can the item-text be customized for the v-select component? I am interested in customizing each item within the v-select dropdown, similar to this example: :item-text="item.name - item.description" ...

Executing the event handler only once

In my React project, I have a button that toggles a boolean state. However, I realized that the button can both set and unset the state due to its toggle functionality. I only want the state to be changed once. Is there a different function I can use ins ...

Sending data from a Flask application written in Python to a JavaScript file using AJAX for seamless integration

As I am still in the learning process, please bear with me. I have been making attempts to find solutions, but often encounter issues when sending data of type="POST" from JavaScript to Flask using AJAX. This is the code snippet from my app.py f ...

Is it possible to vertically center a child div within its parent container using JavaScript when the page loads without explicitly setting its position?

Using JavaScript to vertically center a child div within a fluid container involves calculating the height of both elements and positioning the child div accordingly. However, one issue faced is that the position is not set when the page loads. To solve ...

Sharing data between two components on the same level in Vue.js

I have a situation where I need to transfer data from one component1 to another component2. I am not utilizing vuex or router for this task. The component tree looks like this: -Parent --Component1 --Component2 In the first component1, I am sending an ...

What is the best way to fill HTML tables using an ajax response?

This is a Laravel blade view/page that requires updating without the need to refresh the entire page. The blade.php code functions correctly and retrieves data from a MySQL database, but there seems to be an issue with the AJAX and JavaScript implementati ...

Navigating with Angular 2: Expressing HTML 5 Routing Challenges

I'm currently working on a simple web application using Express 4 and Angular 2. The only Angular 2 specific aspect in this project is the utilization of its HTML5 router. Let me walk you through how the routing works in this app: There are two prim ...

Retrieve the node environment variable within a JavaScript file

When setting the env variable in the start command within the package.json file, here is an example: package.json "scripts": { "start": "webpack-dev-server --config ./webpack.config.config.dev.js --env.mode=dev --port 4200&quo ...

Leveraging server-sent events (SSE) for real-time updates on a web client using JavaScript

I have been experimenting with server-side events (SSE) in JavaScript and Node.JS to send updates to a web client. To simplify things, I created a function that generates the current time every second: setTimeout(function time() { sendEvent('time&a ...

Achieve the elimination of null values from text fields and text areas with the help of

One of the columns in my database table has null values. When I try to save the value using formData: formData.append('remark', this.form.remark); I encounter an issue where the textfield displays null when retrieving the data. <textarea v-m ...

Unable to verify identity using spa

My API is running on the URL: https://example.com, and my vue.js app is running on: https://app.example.com. I have configured the .env file as follows: SESSION_DOMAIN=.example.com SANCTUM_STATEFUL_DOMAINS=https://app.example.com The axios configuration i ...

How can I assign several Objects to a single array?

My goal is to save several objects into an array. Specifically, I have five objects named obj1, obj2, obj3, obj4, and obj5. ...

Eliminate any properties with values that exceed the specified number in size

:) I'm trying to create a function that removes properties with values greater than a specified number. I've searched through multiple resources like this question on how to remove properties from a JavaScript object and this one on removing pro ...

What could be causing my Node.js website to have trouble locating pages in the public directory?

Embarking on my journey in web development using node.js, I encountered an issue while trying to load a particular page, which led to the following error message in my browser: Cannot GET /public/pages/terms-and-conditions.html The file structure is orga ...

Having trouble with Discord.js version 12 and the messageReactionAdd event not triggering properly?

client.on('messageReactionAdd', (reaction, user) => { console.log('If you see this I actually work...'); }); I am having some trouble with my code. Despite setting up a simple console log, it seems like the code is not running prope ...

Global variables across the application in vue.js

Is there a specific way in Vue.js to define application-wide global variables like baseUrl and then use them across the app (for API calls, etc)? If yes, what is the recommended method to set up these globals during build-time for different environments s ...

`Is there a way to retrieve the ID of an element upon clicking on it?`

Can you help me with a JavaScript query? I have two HTML div elements with ids of 'div1' and 'div2'. When I click on any of the divs, I want to display the id of the clicked div. Any thoughts? <div id="div1"></div> <div ...