Having trouble retrieving the array index of JSON data in Vue JS?

I have a project that includes data from a JSON API and I'm trying to display the index of each data. For example, I can view the floors from array index 0 to 22, but I'm having trouble getting the array index for the flats on each floor. Each floor has 6 arrays representing different flats, with each array containing 6 flats. I want to be able to view them from 0 to 6 for each floor, but I'm unable to do so. Can anyone please help me with this?

https://i.sstatic.net/aWzCj.png

<template>
    <b-card no-body class="bg-default shadow">
  
 <b-table-simple responsive>
  <b-thead>
    <b-tr>
      <b-th sticky-column>flats </b-th>
      <b-th >  //here i want to show the indexes of array(6) in provided image
      
      </b-th>
   
    </b-tr>
  </b-thead>
  <b-tbody >
    <b-tr  v-for="(floor,floor_index) in Building.floors"
              :key="floor_index">
      <b-th sticky-column>{{floor_index}}</b-th> //here i viewed from 0 to 22 floors
      <b-td>Cell</b-td>
 
    </b-tr>
  </b-tbody>
 </b-table-simple>

    </b-card>
    
</template>
<script>
  import projects from './../projects'
  import { Table, TableColumn} from 'element-ui'
  import BuildingsService from "@/services/ApiService"
  export default {
    name:<br>'light-table',
    components: {

    },
    data() {
      return {
          Flats:[],
          index:0,
          Floors:[],
          Building:[],
         NoOfFloors: [],
        projects,
        currentPage: 1
      };
    },

mounted: function(){
 
      
      BuildingsService.getOneBuilding(`${this.$route.params.id}`).then((response) => {
      this.Building = response.data.response;
 this.NoOfFloors = this.Building.floors;

console.log(this.Building.floors,"single");
   

    });

        BuildingsService.getFlats().then((response) => {
      this.Flats = response.data.response;
 

    });




    
    }
  }
</script>

Answer №1

Are you attempting to extract only the 6th item out of the 22 in your array, or does each of the 22 items contain a 6-item array within themselves? Just trying to clarify exactly what your objective is.

Edit: I believe I understand what you're aiming for. While I can't test this on my end, here's essentially what you need to do to retrieve an array within another array. You simply run another v-for loop inside the v-for before it.

Give this a try.

Edit: It should be "floor" instead of "floor_index."


     <template>
    <b-card no-body class="bg-default shadow">
  
 <b-table-simple responsive>
  <b-thead>
    <b-tr>
      <b-th sticky-column>flats </b-th>
      <b-tr  v-for="(floor,floor_index) in Building.floors"
              :key="floor_index">
        <div v-for="(flat, index) in floor) :key="index">
               <b-th>{{flat}}</b-th>
                     </div>

      
      </b-th>
   
    </b-tr>
  </b-thead>
  <b-tbody >
    <b-tr  v-for="(floor,floor_index) in Building.floors"
              :key="floor_index">
      <b-th sticky-column>{{floor_index}}</b-th>; //here I viewed from 0 to 22 floors
      <b-td>Cell</b-td>
 
    </b-tr>
  </b-tbody>
 </b-table-simple>

    </b-card>
    
</template>

Another way to further break down the information is like this. This will display the flats (or any other items you have in that array).

<b-tbody >
    <b-tr  v-for="(floor,floor_index) in Building.floors"
              :key="floor_index">
      <b-th sticky-column>{{floor.flats}}</b-th>; //here I viewed from 0 to 22 floors
      <b-td>Cell</b-td>
 
    </b-tr>
  </b-tbody>

You can also display a specific index like this. Here are some examples.

{{floor[5]}} //or whatever index you want 0 to 5
{{floor.flats[2]}} //or whatever index you want 0 to 5
{{floor.flats.rooms[1]}} //or whatever index you want 0 to 5

Etc.

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 dynamically adding events to a tag within a custom grid component

Presenting a query: `<tr @click="rowevent != null?rowevent(row,this.$el):''" :class="index % 2 === 0?bodytrclass[0]:bodytrclass[1]">` I am wondering how I can dynamically add events based on data (props) without explicitly writing each ev ...

Dealing with NULL values in a Postgresql database

In the web-survey I created, users are able to select their answers by using radio buttons. To determine which buttons have been selected, I utilize javascript. I have not pre-set any default states for the buttons, allowing users to potentially leave ques ...

Securing API endpoints in a React/Redux application using proxy techniques

Ensuring the security of my react/redux application is a top priority for me. I've noticed that my api url is exposed to the public inside the bundled app.js file, which raises some concerns. After doing some research, I discovered that some developer ...

What is the method of duplicating an array using the array.push() function while ensuring no duplicate key values are

In the process of developing a food cart feature, I encountered an issue with my Array type cart and object products. Whenever I add a new product with a different value for a similar key, it ends up overwriting the existing values for all products in the ...

Ensure a field is required if the integer field is not empty in Angular Schema Form

I am currently working on setting up a form using Angular JSON Schema Form. My goal is to make one field (dropdown1) required only when another field (number1) has been filled in. I have managed to successfully get the following form + schema to function p ...

How can I achieve the same result as npm run start:ci with yarn?

I need help setting up Bitbucket CI/CD to run cypress tests on my vue app that utilizes yarn as its package manager. Is there a method to launch the server in the background using yarn? Appreciate any assistance. Thanks in advance! ...

Implementing event handling for external modules using on method in Node.js

I have been utilizing the request module (available at https://www.npmjs.com/package/request) for executing a series of http requests in my application. However, I am currently facing an issue while attempting to integrate a logging feature into it. I am s ...

A helpful guide on resetting ReactJs to its default state when data is not found

Currently, I'm fetching data from my database, but just for the sake of this question, I have opted to manually create an example with fake data. I am in the process of creating a search bar for my users to navigate through all the data retrieved fro ...

What is the equivalent of Node's Crypto.createHmac('sha256', buffer) in a web browser environment?

Seeking to achieve "feature parity" between Node's Crypto.createHmac( 'sha256', buffer) and CryptoJS.HmacSHA256(..., secret), how can this be accomplished? I have a piece of 3rd party code that functions as seen in the method node1. My goal ...

"Make sure to tick the checkbox if it's not already selected,

Could use some assistance with traversing and logic in this scenario. Here is the logic breakdown: If any checkbox in column3 is checked, then check the first column checkbox. If none in column 3 are selected, uncheck checkbox in column1. If column1 ...

Could using 'require' in node.js lead to a memory leak issue?

I have been working on a program that experiences continuous heap growth. The program is quite simple - it repeatedly tries to load an external file (SyntaxError) using require. However, this external module fails to load due to a syntax error present in i ...

The content of xmlhttp.responseText is not being displayed in the innerHTML

As part of my ongoing effort to enhance my understanding of Ajax for work purposes, I have been following the W3Schools tutorial and experimenting with my Apache2 server. In this process, I have placed a file named ajax_info.txt on the server (in /var/www ...

Fix a typing issue with TypeScript on a coding assistant

I'm struggling with typing a helper function. I need to replace null values in an object with empty strings while preserving the key-value relationships in typescript // from { name: string | undefined url: string | null | undefined icon: ...

Does angular-sortablejs work with angular 5?

I attempted to use angular-sortables in combination with the ng-drag-drop library to sort the list items that are being dragged, but it appears that nothing is happening when I try to use it. Does anyone know if angular-sortables is compatible with Angular ...

What is the method for determining the gaps between cells in a grid-based puzzle game similar to Sudoku?

this is my current code and it's successfully functioning var cellSize:Number = 36; var cellGap:Number = 4; var row:Number; var col:Number; for (var a:int = 0 ; a < puzzleSTR.length ; a++) { col = a % 9; row = Math.floor(a / 9); ...

Searching for a way to detect when a user clicks the back button on their Android or iPhone device using JavaScript or

In the process of building a Single Page Application (HTML5) utilizing the following plugins: - Sammy.js - Knockout.js - Require.js - Jquery.js This app is designed for Android, iPhone, and Windows mobile devices. Many scenarios revolve around cli ...

What is the best way to access an error's body in order to retrieve additional error message details when using the forge-api with nodejs?

I'm struggling to retrieve the body content when an error is returned from the API request. I've attempted creating a bucket with uppercase letters, but all I receive is an error object with statusCode = "400" and statusMessage = "BAD REQUEST". ...

The middleware in Express.js is failing to execute the next function

I have been attempting to run a post function that contains a next(); within the code. To solve this issue, I have exported the function's definition from another file and am trying to call it through an express router. Unfortunately, the function is ...

Is there a way to input the Sno data into the database in ascending order?

function table_insert(lease_ids){ var lease_id=lease_ids+1; var table = document.getElementById('table_data123'), rows = table.getElementsByTagName('tr'), i, j, cells, customerId; for (i = 0, j = rows.le ...

DataGrid Filtering with Material-UI

I recently started working on a React project and I'm trying to incorporate the mui datagrid component. I want to include filters for '>' and '<', but I couldn't find any information in the documentation. Is there a spec ...