What is the best way to retrieve the color options in the variant using vue js?

I'm trying to loop through the variants and retrieve data from the colors array.

I believe my loop code is incorrect

<span v-for="{items, index} in product.variants" :key="index">
  <em v-for="(item, index) in items.colors" :key="index">{{ item.name }}</em>
</span>

This is my JSON object:

"variants": [
    {
     "size": "39",
     "colors": [
        {
          "name": "Red",
          "stock": 2
        },
        {
        "name": "Black",
          "stock": 1
        }
      ]
    },
    {
     "size": "39",
     "colors": [
        {
          "name": "Red",
          "stock": 2
        },
        {
          "name": "Black",
          "stock": 1
        }
      ]
    }
  ]

Could someone assist me on how to achieve this?

Answer №1

Give this a shot.

<div v-for="(item, index) in product.variants" :key="index">
  <p v-for="(innerItem, innerIndex) in item.colors" :key="innerIndex">{{ innerItem.name }}</p>
</div>

Answer №2

Where can I find your product? Also,

There is an issue with the curly braces in

v-for="{items, index} in product.variants"
. It should be replaced with normal parentheses.

Here is a corrected example:

<template>
  <div>
    <span v-for="(items, index) in product.variants" :key="index">
    <em v-for="(item, index) in items.colors" :key="index">{{ item.name }}</em>
    </span>
  </div>
</template>

<script>
export default {
  data(){
    return {
      product: {
        variants: [
          {
            "size": "39",
            "colors": [
              {
                "name": "Red",
                "stock": 2
              },
              {
                "name": "Black",
                "stock": 1
              }
            ]
          },
          {
            "size": "39",
            "colors": [
              {
                "name": "Red",
                "stock": 2
              },
              {
                "name": "Black",
                "stock": 1
              }
            ]
          }
        ]
      }
    }
  }
}
</script>

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

Rails link_to using remote true property not refreshing the view

I'm currently working on an app that features events. On the index view for events, I loop through each event to display relevant data. Within this loop, there is a link to 'Upvote' a specific event which triggers an ajax request. However, I ...

You cannot access the property 'subscribe' on a void type in Angular 2

fetchNews(newsCategory : any){ this.storage.get("USER_INFO").then(result =>{ this.storage.get("sessionkey").then(tempSessionKey =>{ this.email = JSON.parse(result).email; this.newSessionKey = tempSessionKey; this.authKey =JSON.stringify("Basic ...

A step-by-step guide on how to deserialize a C# object in JavaScript

After doing some research online, I have yet to find a solution to my query. I currently have a C# serialization created from an Object within a MemoryStream. My goal is to save this Serialization (a task I can already accomplish) and then deserialize it ...

Is there a way to access the value stored in a URL parameter if xmlhttp.status shows 500 error?

Hello, I am new to programming and currently working with Javascript and PHP. I have a page that contains several buttons, each of which should link to their respective vendordetails.html page. Once on the vendordetails.html page, a PHP code will be execut ...

resolve CORs issue with api in express app.get( ' /* ')

Can you please explain how to retrieve data from a JSON API? server.js app.get('/*', function(req, res) { res.sendFile(path.join(__dirname, 'public', 'index.html')); }) app.get('/api', function(req, res) { ...

Get the MAC address of a client using Node.js

I have a project in progress that aims to help my home automation system recognize the presence of individuals at home by using their MAC addresses as identifiers. In my attempt to collect the MAC address of a client on my network, I utilized Nodejs along ...

Anticipating the execution of pool.query within a callback function in the Express framework

Within an Express post endpoint, I am utilizing crypto.generateKeyPair. After generating the key pair, I wish to store it in my database and then return the ID of the inserted row within the same endpoint. Here is the code snippet for the endpoint: app.p ...

Utilizing Visuals from a Vue Component Collection

As I attempt to publish a Vue library to npmjs, I encounter an issue with the images within it. After publishing the app to NPM and importing it into my main app, everything appears to be working correctly. However, the paths to the images in the library ...

Utilizing Angular to Toggle Visibility with Button Directives

Within my main view, I have three directives that each display different sets of data. <div> <sales-view></sales-view> </div> <div> <units-view></units-view> </div> Addi ...

What is the proper way to retrieve the value of the key "order" from this JSON object (containing semicolons in the name)?

Vijay Anand inquired about this particular matter yesterday, unfortunately, the discussion was closed before receiving any solutions: HTTP Response: { "entry": { "@xml:base": "https://API_PROC_SRV/", "@xmlns": "http://www.w3.org/2005/Atom", ...

Another option instead of using jQuery to retrieve the active element in the

My goal was to determine when the user is interacting with an INPUT or TEXTAREA element and set a flag variable to true during this engagement. Once the user clicks out of these elements, the flag should be set back to false. To achieve this, I utilized j ...

Enhancing Accessibility of the 'Return to Top' Link

Currently, I am designing a web page that requires users to scroll extensively. To enhance the user experience, I have included a back-to-top link at the bottom of the page for easy navigation back to the top. This is the HTML markup I have implemented: ...

Connect a responsive div to a main div

I'm relatively new to the world of Javascript, CSS, and HTML. Currently, I'm attempting to anchor a div to the center and bottom of its parent div. The parent div contains a responsive background image and my fa fa-arrow icon is correctly positi ...

Important notice: Warning for stand-alone React 18 page regarding the import of createRoot from "react-dom"

I am in the process of developing a standalone webpage utilizing React 18: <!DOCTYPE html> <html lang="en"> <head> <title>Hello React!</title> <script crossorigin src="https://unpkg.com/react@1 ...

Can a MS Teams Bot be triggered to show a botMessagePreview from a task or submit activity rather than a composeExtension or submitAction activity?

As I develop a messaging extension in Teams that utilizes task modules and sends adaptive cards, I am faced with the challenge of invoking the same task module from both a messaging extension command and a button on an adaptive card sent to the user. The ...

Enhance Your Jekyll Site with the Minimal Mistakes Theme Plugin

Hi there! I'm relatively new to website programming and could really use some assistance. I've been attempting to integrate the jekyll-lunr-js-search (https://github.com/slashdotdash/jekyll-lunr-js-search) into the minimal-mistakes theme, but I&a ...

Tips for adding up data tables in CodeIgniter

The output is displaying as $NaN ( $NaN total) I followed the code example from datatables and made modifications to the column for SUM function. Here is my code snippet: <script> $(document).ready(function() { $('#tableoperasional').Data ...

Using conditional statements in a forEach iteration

Looking to Apply a Unique Function to a Specific Object in an Array In my current project, I have set up a forEach loop to iterate through various records from a database. Each record generates a 'panel' on the interface. Preview of the panels: ...

Accessing AngularJS variable scope outside of a function

Utilizing a socket, I am fetching data into an angularJS controller. $rootScope.list1= ''; socket.emit('ticker', symbol); socket.on('quote', function(data) { $rootScope.list1 = angular.fromJson(data.substring(3)); //I can ...

Configuring Proxy Settings for WebpackDevServer

I need assistance setting up a proxy using WebpackDevServer in my React/Node chrome extension. Currently, my server is running on localhost:4000, and the React frontend is on localhost:5000. When trying to access the route /api/user/ticket using Axios, I ...