Vue components display images duplicated

My website features a news section where each card and image appear twice on the screen.

Visit my CodePen profile for more information.

One of the components responsible for this is called doubleUp

   created() {
    this.doubleUp()
   },

I'm wondering if these two lines of code are causing the duplication:

    newImage.id  = image.id * 2
        this.images.push(newImage)

I've experimented with changing the multiplication value to *1 or *6, but it doesn't affect the number of cards displayed. I'm puzzled as to why the images keep rendering twice.

Answer №1

The doubleUp function is designed to display your images twice by duplicating the existing this.images array and pushing each image again in a loop.

doubleUp() {
  //fake a bunch of data
  let localImages = JSON.parse(JSON.stringify(this.images))

  localImages.forEach((image) => {
    let newImage = image
    newImage.id  = image.id * 2
    this.images.push(newImage)
  })

  setTimeout(() => {
    this.loading = false
    //then run replacePlaceholders
    this.replacePlaceholders()
  }, 200)
},

If you only want to double up the id values in the images array without adding duplicate images, follow these steps:

doubleUp() {
  //fake a bunch of data
  let localImages = JSON.parse(JSON.stringify(this.images))
  this.images = []; // <------------------------
  localImages.forEach((image) => {
    let newImage = image
    newImage.id  = image.id * 2
    this.images.push(newImage)
  })

  setTimeout(() => {
    this.loading = false
    //then run replacePlaceholders
    this.replacePlaceholders()
  }, 200)
},

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

passport.js and express: TypeError - Router.use() must be given a middleware function, but instead received a ' + gettype(fn)

I encountered an error while using passport.js with Express.js TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) This issue arose from the following code: passport/local.js var LocalStrategy = require("passport ...

"Comparing JSON objects can be done by checking if their IDs match, and if they do, assigning the value to another object

I am working with two sets of data var JSON_Categories = '[{ "id" : "1", "text" : "Category A"}, { "id" : 2, "text" : "Category B" }]'; var JSON_Article = '[{ "id&quo ...

Is it possible to replace the prototype of an object with a different object?

When an entity is generated, its prototype is established as another entity. Is it possible to alter the prototype of a previously created entity to point to a different object? ...

Setting up child routes can be easily accomplished by following these steps

I've been exploring the functionality of child routes within VueJS. My assumption was that setting up a news overview with links to individual news items and using a child route would allow me to view each item separately. However, it's not worki ...

Is it possible for an Ajax request to finish before the DOM finishes loading?

Currently, I am fetching data via a jQuery Ajax request and presenting it on the page. In order to properly display the information, I have to make sure that both the DOM is fully loaded and the Ajax call has been completed. Is there a scenario where an ...

Create a rectangle on a canvas using coordinates that have been translated relative to the original image

Displayed on the screen is an image element that undergoes a css transform: rotate(degree), with degree ranging from 0 to 360. This transformation makes the image appear rotated, even though its original orientation remains unchanged. Given this scenario, ...

Issue with Ionic toggles not updating the correct local storage entry

Encountering an issue with ionic toggles where changing any toggle affects only the last if statement below in local storage. Here is the list of toggles... $scope.settingsList = [ { text: "GBP", checked: gbpON }, { text: "USD", checked: usdON } ...

Exploring the power of jQuery's deferred method and utilizing the ajax beforeSend

Using the deferred object in $.ajax allows for: Replacing the success-callback with the deferred-method done() Replacing the error-callback with the deferred-method fail() And replacing the complete-callback with always() When using: var jqxhr = $.ajax ...

The binding style in vue.js using the ternary operator is throwing an error: Unexpected token ']'

partNav = Vue.component('part-nav', { data: navItems: [ { subItems: [ {...} {....} ] } {...} # another object in navItems array ] template: ' <div v-for="(navIte ...

Ways to activate offline assistance with HTML5 history api

Exploring the best strategies to support offline mode using the html5 history api for URL rewrites. How can this be effectively implemented? Consider a hypothetical scenario where a PWA SPA application is hosted at https://abc.xyz, featuring international ...

wiping out a column in Excel spreadsheets with the help of Node.js or its corresponding node modules

I've attempted various approaches without success. Can you provide guidance on deleting and adding columns within an Excel sheet using Node.js? ...

How can I create a notification popup triggered by a button click with Ajax and Javascript?

I'm in the process of developing a website that involves notifications, but I am unsure how to display them when a button is pressed. For example, clicking "Show Weather" should trigger a notification to appear in the bottom corner of the page. https ...

What is the best way to pass a module from the controller to a Jade/Pug template and then use it as an argument in an event handler within the template?

I passed the module 'naija' to a template from the controller in this way: res.render('testing', {title:"filter setting page", nigeria:naija }); The func ...

The header in datatables.net is positioned on the side of the table rather than at the top

I am currently facing an issue with my datatables.net datatable. While all the columns and header are displaying properly, I am unable to position the header on top of the table. Instead, it appears on the side. Here's a visual representation: Curren ...

Encountering an issue while attempting to generate a dialog popup with jQuery

As a beginner in jQuery, I am attempting to implement a popup dialog box for users in case of an error. However, I'm encountering issues with the following jQuery code: <script type="text/javascript"> $(document).ready(function() { var $dia ...

Ext JS - A grid cell containing varying values, accompanied by a selection of combo boxes

I'm currently implementing the Ext JS Grid component and have a list of fields with their respective data types: ID (int) Name (string) Foods (List<string>) Each user can have multiple foods, selected from a Food DataStore. Displaying this in ...

JQuery.each is causing an issue due to a potential conflict between JSON and string data types

Hey there, I'm trying to loop through each title in this code but encountering an error in the console that says "cannot use 'in' operator." Everything works fine when I provide an ID from the database. However, when I try to pass in a strin ...

JavaScript is throwing an error because `csv[i]` has not

When I try to import a CSV file using JavaScript, the dataset imports successfully into my program. However, I keep encountering the error csv[i] not defined (line 57). I attempted to add var i = 0; just before that line, but the error persists. Any sugg ...

How can I check if the HTML syntax in Vue is valid?

My component takes input and renders the HTML content: <template> <div> <input type="text" v-model="html"> <VRuntimeTemplate :template="html" /> </div> </template> <script> import VRuntime ...

Parent component failing to capture events triggered by child component

Check out this demo showcasing what I'm anticipating to work: https://jsfiddle.net/qe766xn0/4/ The issue here is that the child component is triggering an event that the parent component isn't able to catch. Based on insights from this resource ...