Displaying only a single JSON object when using V-for

I'm currently facing an issue with my v-for loop, where I am passing two JSON objects but only the values from the first object are being displayed on the screen. My JSON data is stored locally in an external .json file and is not fetched through an API.

        <b-card-body v-for="municipio in municipios.centro_oeste.rio_vermelho,municipios.centro_oeste.:key="index">
    <b-card-text>{{ municipio.title }}</b-card-text>
        </b-card-body>  
Using (obj1,obj2) in v-for only shows obj1...  

JSON:

{
  "centro_oeste": {
    "rio_vermelho": [
      {"title": "Goiás"},
      {"title": "Araguapaz"},
      {"title": "Aruanã"},
      {"title": "Britânia"},
      {"title": "Faina"},
      {"title": "Guaraita"},
      {"title": "Heitoraí"},
      {"title": "Itaberaí"},
     {"title": "Itapirapuã"},
      { "title": "Itapuranga"},
      { "title": "Jussara"},
      { "title": "Matrinchã"},
      { "title": "Mossâmedes"},
      { "title": "Mozarlãndia"},
      { "title": "Nova Crixas"},
      { "title": "St° Fé de Goiás"}
    ],
    "oeste_1": [
      {"title": "Amorinópolis"},
      {"title": "Aragarças"},
      {"title": "Arenópolis"},
      {"title": "Baliza" },
      {"title": "Bom Jardim de Goiás"},
      {"title"": "Diorama"},
      {"title": "Fazenda Nova"},
      {"title": "Iporá"},
      {"title": "Israelândia"},
      { "title": "Ivolãndia"},
      { "title": "Jaupaci"},
      { "title": "Moiporá"},
      { "title""": Montes Claros de Goiás "},
      { "title""""Piranhas"}
    ]
} 

obj1+obj doesn't work as well!
What should I do to concatenate these two objects?

Answer №1

To start, it is important to establish the index before utilizing it in your code. Utilizing the spread operator can help combine two lists seamlessly. Incorporating a computed property will enhance the organization of your code:

new Vue({
  el:"#app",
  data: () => ({
    municipios: {
      "centro_oeste": {
        "rio_vermelho": [ {"title": "Goiás"}, {"title": "Araguapaz"} ],
        "oeste_1": [ {"title": "Amorinópolis"}, {"title": "Aragarças"} ]
      }  
    } 
  }),
  computed: {
    centro_oeste_list: function() {
      return [...this.municipios.centro_oeste.rio_vermelho, ...this.municipios.centro_oeste.oeste_1];
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <div
    v-for="(municipio, index) in centro_oeste_list"
    :key="index"
  >
    <p>{{ municipio.title }}</p>
  </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

Tips for preloading a small placeholder image before the main content is loaded

After browsing , I noticed an interesting image loading style. The website initially shows a patterned image before revealing the actual content. This method creates visually appealing content while waiting for the entire webpage to load. Upon inspecting ...

Angular Code Splitting with Webpack

My current project setup is causing some loading issues due to the large download size of Angular Material. As a result, a white screen remains loading for around 45 seconds. I have attempted to implement code splitting to enhance the loading speed of my a ...

ESLint Issue: Every item in a list must be assigned a distinct "key" property

Below is a JSON string with line breaks indicated by '\n': data:{a:"A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations automatically via computer programming. Modern computers have the ...

Is there a more efficient method for examining each multidimensional array?

I have created a program that converts JSON to PHP and checks every value of a multidimensional array with a foreach loop. If a value meets a certain condition, it deletes that key and value and then converts it back to JSON. I am trying to achieve this wi ...

Creating an import map using jspm2 can be done by following these steps

Currently, my goal is to utilize JSPM module loader to import javascript packages from npm instead of CDN and employ an offline package loader. Now, the next step involves incorporating an importmap script in order to successfully import modules like rea ...

Undefined value retained in $scope variable despite being assigned

I am facing an issue with a function in one of my Angular controllers. Within the function, I check if a departure time has been provided. If it hasn't, I assign the current time to it in a HH:mm:ss format. Even after assigning a value to $scope.sel ...

transferring information between pages in nextjs

Currently in the process of developing a website, specifically working on a registration page for user sign-ups. My main challenge at the moment is validating email addresses without using Links. I need to redirect users to a new page where they can see if ...

What is the best way to add randomness to the background colors of mapped elements?

I am looking for a way to randomly change the background color of each element However, when I try to implement it in the code below, the background color ends up being transparent: { modules.map((module, index) => ( <div className='carou ...

Managing information from various selection fields

I am trying to work with a multiple select field in HTML that looks like this: <select name="deductions[]" multiple="multiple"> <option>Option 1</option> <option>Option 2</option> <option>.......</option> </sel ...

Generate a new entry for a singular piece of data within an array

I am working on a matchmaking system where two players of the same level are matched and joined in one array. My goal is to include a second data in the array for players who do not have a match. Example: EntryID: “15”, player: ”testing11”, level: ...

Identify the invalid field within an Angular form

After the form is rendered, I need to identify which fields are not valid after 5 seconds. Currently, I have a button that is set as ng-disabled="!step1Form.$valid". However, I would like to add a CSS class, possibly in red, to highlight the invalid fields ...

What causes an error when bootstrap.js is loaded twice?

I recently encountered a problem while using summernote as my online web editor. I discovered that certain features of the editor, specifically buttons with the bootstrap dropdown class, were not functioning properly. After some investigation, I realized ...

Using Javascript to dynamically add variables to a form submission process

Looking to enhance my javascript skills, I've created a script that locates an existing id and exchanges it with a form. Inside this form, I'm aiming to incorporate javascript variables into the submit url. Unsure if this is feasible or if I&apo ...

Language for describing JSON data structures

Imagine my application needing to access data from multiple REST APIs, each supporting JSON responses but varying in the fields used to describe the data. For instance, one API may use time, while another may use timestamp for timestamp data, and similar v ...

Could someone provide some clarification on this callback related to node.js?

With the abundance of node.js tutorials available showing how to create a server, it can be overwhelming as they are all coded in different ways. The question then arises - when should you write it one way versus another? Unfortunately, none of the tutoria ...

When can JavaScript objects hold up and when do they meet their demise?

Just diving into the world of Javascript, I stumbled upon an intriguing article that discussed the concept of reusing an ajax connection multiple times. The article gave an example: "You can define an ajax connection once, and reuse it multiple times, s ...

Is there a way to retrieve a formatted address from a JSON decode output?

I am attempting to retrieve the formatted address from a specific URL. Even after using the code below, I am unable to display anything and no errors are being shown. How can I extract the formatted address from the JSON decoded result? foreach($result ...

Convert Parse.com data into a JSON string format and then reverse the process

Currently, I am utilizing PushWoosh for sending custom data and would like the capability to send a ParseObject from one user to another. It seems that this can only be done by converting the ParseObject into a JSON string first, and then converting it bac ...

Responsive Tabs with Material-UI

Can MUI's Tabs be made responsive? This is what I currently have: https://i.stack.imgur.com/KF8eO.png And this is what I aim to accomplish: https://i.stack.imgur.com/b3QLc.png ...

A versatile jQuery function for extracting values from a :input selector

Is there a universal method in jQuery to retrieve the value of any :input element? I pose this question because I have a webpage containing select and checkbox inputs, as seen in the code below: for (var i = 0; i < arguments.length; i++) { ...