Exploring Vue.js: Navigating Through an Array of Images Nested Within Another Array

I am looking to showcase images stored in an array called "image" within another array named product.

Essentially, if a product has an array of 3 images, I want to display all 3 images, and so on.

Here is the code snippet:

<template>
  <div class="details">
    <div class="container">
      <div class="row">
        <div class="col-md-12" v-for="(product,index) in products" :key="index">
          <div v-if="proId == product.productId">
            <h1>{{product.productTitle}}</h1>
            <h2>{{product.productId}}</h2>
            <img :src="product.image[0]" class="img-fluid">
          </div>
        </div>
        <div class="col-md-12" v-for="(product,index) in products" :key="index">
          <div v-if="proId == product.productId">
            <img :src="product.image[1]" class="img-fluid">
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "details",
  data() {
    return {
      proId: this.$route.params.Pid,
      title: "details",
      products: [
        {
          productTitle: "ABCN",
          image: [
            require("../assets/images/autoportrait.jpg"),
            require("../assets/images/bagel.jpg")
          ],
          productId: 1
        },
        {
          productTitle: "KARMA",
          image: [require("../assets/images/bagel.jpg")],
          productId: 2
        },
        {
          productTitle: "Tino",
          image: [require("../assets/images/bagel2.jpg")],
          productId: 3
        },
        {
          productTitle: "EFG",
          image: [require("../assets/images/bagel3.jpg")],
          productId: 4
        }
      ]
    };
  }
};
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

I have successfully displayed information from the first array like product title and product id. However, I have duplicated my code in the vue template to display more images from the second array by changing the index values "product.image[0]", "product.image[1]".

Is there a more efficient way to achieve this?

Thank you for your assistance!

Answer №1

To display a series of product images, you can utilize the v-for directive in Vue.js. This allows you to loop through each image similar to how you iterate through products:

<div class="col-md-12" v-for="(product, index) in products" :key="index">
    <div v-if="proId == product.productId">
        <h1>{{product.productTitle}}</h1>
        <h2>{{product.productId}}</h2>
        <div v-for="(image, imageIndex) in product.image">
            <img :src="image" class="img-fluid" :key="imageIndex" />
        </div>
    </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

Does Vue3 support importing an HTML file containing components into a single file component (SFC)?

I am working on a Form-Component (SFC) that is supposed to import an HTML file containing Vue components. Form-Component <template src="../../../views/form-settings.html"></template> <script setup> import Button from "./. ...

VueJs Multipage Application

Hello there! I'm undertaking the development of a large-scale VueJs Application and I want to structure it as a Multipage Application with two distinct sections - Admin and User. These sections should load independently while sharing some component ...

Securing pathways and pages using NextJs

I recently completed a project where I implemented route protection for a website using the if and else statements, assigning each page a function withAuth(). However, I have concerns about whether this is the most effective method for securing routes in n ...

Can icons with an external path be utilized as a src in the manifest.json file within an Angular application?

Let's visualize the setup with two projects - project1 and project2. Each project has its own manifest.json file and the same apple-touch-icon-144x144.png file located in assets/icons directory. -project2 |_src | |_assets | | |_icons | | ...

I am curious about the process of using Mongoose to insert key-value pairs as objects into an array through a form. Can you provide

Here is a simple schema to consider: var PostSchema = new Post({ body : String, tags : [] } I am currently developing a REST HTTP API form POST, and I want to populate the tags array with key/value pairs. My goal is for the mongodb document to appear ...

Enabling the submit button only when text is entered in the text fields using jQuery

I have a script that automatically submits a form when two textfields are filled using variables from local storage. The script checks if the variables for email and password are not null before submitting. if (localEmail != null && localPwd != null) { ...

Button Click Not Allowing Webpage Scroll

I am currently in the process of developing a website that aims to incorporate a significant amount of motion and interactivity. The concept I have devised involves a scenario where clicking on a button from the "main menu" will trigger a horizontal and ve ...

Refresh Vue/Nuxt Components Fully

Understanding how this.$forceUpdate() functions, I am not simply looking to re-render the component. In Nuxt applications, page components have asyncData() as a lifecycle method that runs before created(). I utilize this method to retrieve initial data an ...

Can you identify the issue in this PHP script?

After creating a web form for inserting data into a mysql database, I encountered an issue when hitting the submit button. Despite having the correct hostname and other database details, the data was not being inserted as expected. It's likely that t ...

Nuxt.js - Which specific files and directories are required for deploying the production version on a hosting platform?

Visit this link for more information I am facing a challenge in deploying Nuxt to live hosting as there seems to be no clear indication of the required files. When using Nuxt CLI, it generates empty folders like: - assets - components - layouts - ...

HTML steps for smooth scrolling to the top and bottom of a list

My HTML contains a vertical list with top and bottom arrows positioned nearby. I am looking to implement a function where clicking on the top arrow will move the list up gradually, and clicking on the bottom arrow will move the list down step by step. Belo ...

using javascript to create two choices

I am facing a small complication between two select options in my javascript code. Here is the snippet of my javascript: function displayVals() { var singleValues = $("select option:selected").text(); //to stay selected $("#hiddenselect").val(s ...

The tooltip in chart.js stubbornly holds onto past data even after it's been

Utilizing chart.js, I have implemented a feature where a tooltip displays when a user hovers over the chart successfully. However, I encountered an issue. I added an option for users to un-check data-points, which works correctly. But now, the tooltip fun ...

Rails Ajax form submission not working

I recently set up a basic Google form on my website and used this website to extract the HTML code for display. However, upon hitting submit, nothing happens - the page seems to be frozen. I'm attempting to redirect the form submission to another page ...

What is the best way to remove duplicate values from my array concatenation function?

Currently, I have a method that is able to merge two arrays in ascending order. However, I still need to find a way to remove any duplicate elements. Here's the code snippet for reference: public static int[] mergeArrays(int[] a, int[] b){ int[] ...

Exploring the concepts of arc functions in discord/js programming

Example: Having trouble understanding how to create an arc in JavaScript based on a percentage input? Here's a simple explanation: feed it a percentage and it will create an arc that represents that percentage of a circle. I've managed to get t ...

Utilize Laravel Echo and broadcasting to generate interactive channels

I'm currently working on implementing a chat feature that allows users to engage in 1 to 1 conversations or group conversations. The technologies I am using for this project are Laravel 5.5 and Vue.js. After reviewing the documentation, I discovered ...

Add a plugin to the website after the DOM has finished loading

Here is a code snippet that utilizes a jQuery plugin to apply scrollbars to a DOM element: <script type="text/javascript"> $(document).ready(function () { $(".pp-meta-content").customScrollbar(); }); </script> This code works ...

Upgrading an Express 2.x application to Express 3.0

I am currently studying NodeJs and Express and am in the process of converting a tutorial app from Express 2.5.9 to version 3.0. The following code is now causing an error "500 Error: Failed to lookup view "views/login". How can I update this to render cor ...

Node.js encountering req.body as undefined when using form-data as the content-type

After creating a small demonstration for this form-data passing API, I attempted to test it using Postman. However, I encountered an issue where no data was being retrieved. Code const http = require("http"); const express = require("expres ...