tips for effectively utilizing getters in array sorting operations

I've been encountering some issues with vuex getters.

My objective is to arrange the cart data, which consists of an array of objects, by date using the getter named myCartItems.

The problem I'm facing is that when I add a second payload

{prod_id: 2, date: Wed Nov 03 2021 10:40:20}
in my Details component, the props['prod_id'] ends up logging as prod_id: 1 instead of prod_id: 2

Mutation:

addToCart: (state, payload) => {
  state.cart.push(payload) // payload = {prod_id: 1, date: Wed Nov 03 2021 10:37:26}
}

Getters:

 cartItems: (state) => {
   return state.cart.slice().sort(
      (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
    );
 },

HTML:

  <div v-for="(item,index) in cartItems" :key="index">
       <Details :id="item.prod_id" />
  </div>

Answer №1

The issue lies in using the index as the key since it does not change with sort order.

A better practice is to utilize a unique identifier such as prod_id.

<div v-for="item in cartItems" :key="item.prod_id">
  <Details :id="item.prod_id" />
</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

Exploring the possibilities of utilizing dynamic components in Nuxt.js 3

I am attempting to display dynamic components but it is not functioning as expected. <component :is="comp" /> The variable comp contains the name of the component during the process (CardComponent). No error messages are being shown and n ...

Incorporating customizable data in Tailwind CSS 3 with Vue 3

Creating a dynamic arbitrary string in TailwindCSS, such as hue-rotate-[${randomHueColor}deg], is not feasible because the string needs to exist at build time. Even passing the generated string through a prop to a different component seems implausible. F ...

HTMLElement addition assignment failing due to whitespace issues

My current challenge involves adding letters to a HTMLElement one by one, but I'm noticing that whitespace disappears in the process. Here's an example: let s = "f o o b a r"; let e = document.createElement('span'); for (let i ...

Determining when a checkbox changes state using HTML and JavaScript

My main objective is to display divX2 when the checkbox for x2 is checked, either by directly clicking on x2 or by clicking on the "Check All" checkbox. The functionality works as intended when the checkbox for x2 is clicked, but it fails to work when the ...

Enable the parsing of special characters in Angular from a URL

Here is a URL with special characters: http://localhost:4200/auth/verify-checking/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="59663c34383035643230383d2b606a6e6b686d6e6e193e34383035773a3634">[email protected]</a> ...

How can we minimize the data contained in JSON HTML markup?

https://i.stack.imgur.com/mzuB0.png Currently, I am attempting to find a way to conceal the last 12 digits in the price shown on a button, as it is excessively long. The method I am utilizing involves a JSON api and insertAdjacentHTML markup. This snipp ...

Ways to create a smooth transition in element height without a known fixed target height

Is it possible to create a smooth height transition for an element when the target height is unknown? Replacing height: unset with height: 100px allows the animation to work, but this presents a problem as the height needs to be based on content and may v ...

What is the best way to choose just the items with a distinct key from within this jQuery assortment?

[Object, Object, Object, prevObject: jQuery.fn.init[3], context: undefined] 0: Object 1: 1 2: 2 3: 7 __proto__: Object 1 : Object 1: 6 2: 2 3: 5 6: 15 __proto__: Object 2 : Object 1: 3 2: 2 3: 5 5: 7 ...

Enter your text in the box to search for relevant results

I need assistance with a code that allows me to copy input from a text box to the Google search box using a copy button. Upon pressing the Google search box button, it should display the search results. Here is the code I have been working on: http://jsf ...

Juicer- Setting restrictions on the frequency of posts

How can I limit the number of posts displayed using the react-juicer-feed component? import { Feed } from 'react-juicer-feed'; const MyFeed = () => { return ( <Feed feedId="<feed-id>" perPage={3} /> ...

JSX refusing to display

Currently, I am enrolled in an online course focusing on React and Meteor. Despite successfully registering a new player in the database upon hitting the submit button, the client side fails to display the information. Upon checking the console, the foll ...

Challenge encountered when attempting to remove elements from an array within a for loop

There seems to be an issue with deleting elements from an array within a for loop. var div = document.querySelector('div'); var array = [ "iPad", "iPod", "iPhone" ]; for (let i = 0; i < array.length; i++) { let p = document.createElement ...

The instance does not have a defined property or method called "msg" but it is being referenced during the rendering process

While experimenting with Vue and Vuex, I encountered this error message: [Vue warn]: Property or method "msg" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for ...

Activate scroll function exclusively upon hovering over specific object

I am working on a page with an object that allows users to zoom in and out. I want to create a special zoom function that will be triggered when the user scrolls while their cursor is hovering over this object. If the cursor moves away from the object, th ...

Learn how to execute the dialog content destroy function once a Vuetify dialog is closed

I'm a beginner with Vuetify and I have noticed that, currently, there is no built-in method to completely remove the body of a dialog when it's closed. Does anyone have a workaround for this issue? When dealing with forms, we can reset field val ...

Enhance the background property in createMuiTheme of Material-UI by incorporating additional properties using Typescript

I've been attempting to include a new property within createMuiTheme, but Typescript is not allowing me to do so. I followed the instructions provided here: https://next.material-ui.com/guides/typescript/#customization-of-theme I created a .ts file ...

Tips on aligning a div to the right of a headline

My goal is to have an orange circle image placed to the right of my headline <h2>. However, I am facing an issue where the circle jumps above the headline when I shrink the screen. How can I ensure that it stays on the right side no matter the screen ...

Issues with Vuetify visibility class functionalityStringReflects Challenges with the Visibility

As per the documentation found here, I'm attempting to utilize a Vuetify visibility class to hide an element on smaller screens. Unfortunately, when using the following class, it has the opposite effect - removing the element on larger screens and di ...

Angular 2: Troubleshooting Issues with Observable Data Display

Looking to implement a RESTful call with Angular 2 that constantly updates whenever there are changes in the API. In my service, I've included an Observable to fetch data from the API: getData(): Observable<any[]> { return this.http.get(url) ...

"click on the delete button and then hit the addButton

I have created a website where users can save and delete work hours. I am facing an issue where I can save the hours at the beginning, but once I delete them, I cannot save anything anymore. Additionally, when I reload the page, the deleted data reappears. ...