How can VueJS cycle through the arrays within an object?

Is there a way to efficiently iterate through arrays within an object like in this example?

I have a simple code snippet that currently outputs indexes in order, but I'm looking to access the values of the array instead.

<template>
  <div>
    <form class="production">
      <div v-for="(id, index) in ids" :key="index" class="form-group">
        <label class="control-label" v-bind:for="ids[index]">{{ labels[index] }}</label>
        <div class="">
          <select v-bind:id="ids[index]" v-bind:name="ids[index]" class="form-control">
            <option v-for="(option, prop, val) in options" :key="val" v-bind:value="option">{{[index]}}{{[val]}}</option>
          </select>
        </div>
      </div>
    </form>
  </div>
</template>

<script type="text/javascript">
export default {
  name: "Production",
  data() {
    return {
      labels: ["Amount", "Country", "Packing"],
      ids: ["amount", "country", "packing"],
      options: {
        amount: ["100", "300", "500"],
        country: ["Germany", "Austria", "Switzerland"],
        packing: ["Option1", "Option2", "Option3"]
      }
    };
  }
};
</script>

I couldn't find much information on this issue, so I assume my approach might be incorrect. If so, could you guide me on how to structure my values properly?

I am new to vueJS and don't have extensive experience with JavaScript. In regular JavaScript, I could achieve the desired output with code similar to this:

for (var x = 0; x < Object.values(options).length; x++) {
    for (var i = 0; i < Object.values(options)[x].length; i++) {
        console.log(Object.values(options)[x][i])
    }
}

VueJS does seem a bit more intricate in handling such tasks.

Answer №1

When using your second v-for, make sure to iterate through the specific data in options[id] instead of the entire options object.

Consider updating your second v-for like this:

<option v-for="(option, prop, val) in options[id]" :key="val" v-bind:value="option">{{[index]}}{{[val]}}</option>

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

Ways to update a component's state by clicking a button located on a different component

Seeking help with changing a parent state value when clicking a button. Although there are numerous similar queries out there, I'm really struggling to grasp the concept. I've attempted lifting the state up but haven't quite nailed it yet. ...

Restricting Checkbox Choices with JavaScript by Leveraging the forEach Function

I am developing a checklist application that limits the user to selecting a maximum of three checkboxes. I have implemented a function to prevent users from checking more than three boxes, but it is not working as expected. The function detects when an ite ...

Working with Node.js and MySQL can involve using callbacks within nested queries

I am trying to add data to my database only if it doesn't already exist. While attempting this, I encountered an error message: { [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false } My ...

How can I generate rotating images using jQuery or JavaScript similar to the one shown here?

http://www.example.com/ On a website similar to example.com, I noticed that when hovering over one of the topics listed, the image rotates. I am interested in creating this interactive effect using either jQuery or JavaScript. Is there a method to access ...

Retrieve information from a local API using Next.js

While working with Next.js api today, I encountered an issue. I needed to fetch data from my internal API in getStaticProps. However, the documentation advises against fetching local API directly in getStaticProps and instead suggests importing the functio ...

Tips for effectively showcasing JSON in an Angular directive

I am facing an issue with my Angular app that utilizes ui-router. I have set up a service and directive, but I am unable to display JSON data. Surprisingly, it works perfectly fine without the directive when I directly display it in the main template (home ...

Guide to replacing a value in a div within a personalized dropdown using VUE JS

Hey there, I've run into an issue. I need to change the "selected" value when I choose an option from a drop-down list. Any ideas on how to do this? <div class=" select__body" v-if="addedForm === 2"> ...

Regenerating VueJS application prior to executing docker-compose up

Looking to deploy a VueJS app within a Docker Nginx container, but before running the container, the VueJS source needs to be compiled using npm run build. The goal is for the compilation process to happen within a container and then exit, leaving only the ...

Retrieve information from a JSON document

My project involves a bookStore with a list of books that I am working on. I am trying to extract all the book data from a JSON file and display them in a card view on a webpage. Although my code is error-free and seems to be functioning properly, the da ...

Using React-Router v6 to pass parameters with React

My App.js file contains all the Routes declarations: function App() { return ( <div className="App"> <Routes> <Route path="/"> <Route index element={<Homepage />} /> ...

Printing in Firefox is ineffective, but functions smoothly in alternative browsers

I'm currently working on customizing some content specifically for printing, so I've implemented a hook import { useState } from 'react' const usePrint = () => { const [isPrinting, setIsPrinting] = useState(false) const hand ...

An easy guide to sorting outcomes using JSON

I have JSONResults in my Controller that contains all the data from a table. On the client's HTML detail view page, I am using JavaScript to fetch this data. How do I extract data from JSON where the client name is equal to klID (this is a JSON string ...

What is the best way to reference a component from another component in a React application?

I've been utilizing the react-notification-system library in my project, and here's a snippet of how I've incorporated it into my code. import React from 'react'; import Notification from 'react-notification-system'; cl ...

Bandcamp API sales data retrieval feature

Looking for assistance with a call to the Bandcamp API. Every time I request /http://bandcamp.com/api/sales/1/sales_report/, I receive this message in the response: /"error_message":"JSON parse error: 757: unexpected token at ''/ ...

What are the steps to successfully install OpenCV (javascript edition) on Internet Explorer 11?

I'm currently experiencing issues with getting the OpenCV javascript version to function properly on IE11 for contour detection. While my code runs smoothly on all other up-to-date browsers, I am encountering errors such as: TypeError: Object doesn&a ...

Data loading issue with asp.net mvc Ajax

I am currently facing an issue while attempting to fetch data from the controller using ajax. Controller [HttpGet] public ActionResult GetServices() { var data = _bbdb.ListServices.Where(d => d.Status == true).ToList(); return Json(data, JsonR ...

Which is better for creating a gradual moving background: Javascript or CSS?

I'm attempting to discover how to create a background image that scrolls at a slower pace than the page contents. I'm currently unsure of how to achieve this effect. A great example of what I'm aiming for can be seen here Would this require ...

Encountering issues when using react-leaflet in a React project with webpack integration

I've been attempting to import react-leaflet into my project without actually rendering any maps, but I keep getting this error message. TypeError: Object(...) is not a function I am certain that the issue stems from the import statement, as indica ...

Save various uploads according to their designated folders in the MySQL database

After browsing through several similar questions to mine, I realized that I need to change the way I am currently storing files in the database. In my first file (getPic.php), I have a form like this: <form enctype="multipart/form-data" method="POST" ...

Display a sublist when a list item is clicked

I am receiving a JSON object in my view that looks like this: $scope.mockData = [ { "folder": "folder1", "reports": [{ "name": "report1" }, { "name": "report2" }, { "name": "report3" }] }, { "folder": "folder2", "reports": [{ "name": ...