The type does not contain a property named `sort`

"The error message 'Property sort does not exist on type (and then shoes4men | shoes4women | shoes4kids)' pops up when attempting to use category.sort(). I find it puzzling since I can successfully work with count and add a thousand separator, for instance. This is within a Vue (.ts) environment.

<div v-for="(count, category) in products[product]" :key="category">
   <div>{{ category }}</div>
   <div>{{ Number(count).toLocaleString() }}</div>
</div>
const products = {
  "shoes": {
    "shoes4men": 1402,
    "shoes4women": 99,
    "shoes4kids": 3000,
    "shoes4athletes": 57
    }

Answer №1

If we consider the scenario where "category" is a string because it acts as the key in a dictionary.

Let's think of this as "value and key" instead of "count and category":

<div v-for="(value, key) in products[product]">

To facilitate sorting, you can first convert to an array, sort it, and then use it with v-for:

<div v-for="(product) in Object.keys(products).sort()" :key="product">
    <div v-for="(category) in Object.keys(products[product]).sort()" :key="category">
        <div>{{ category }}</div>
        <div>{{ Number(products[product][category]).toLocaleString() }}</div>
    </div>
</div>

For more information, visit: https://vuejs.org/guide/essentials/list.html#v-for-with-an-object

Note: Avoid performing computations like sorting directly in the template as they can be costly, as pointed out by Estus Flask.

If you don't need direct access to the category with syntax like:

Products["shoes"]

You can organize data in a sorted manner using a different structure, for example:

const products: Category[] = [
    {
        "name": "shoes",
        "categories": [
             { "name": "shoes4athletes", "value": 57 },
             { "name": "shoes4kids", "value": 3000 },
             { "name": "shoes4men", "value": 1402 },
             { "name": "shoes4women", "value": 99 }
        ]
    }
]


<div v-for="(product) in products" :key="product.name">
    <div v-for="(category) in product.categories" :key="category.name">
        <div>{{ category.name }}</div>
        <div>{{ Number(category.value).toLocaleString() }}</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

What is the process for importing a JavaScript file into a Vue component?

Having trouble importing JSON results into a Vue component? The results are as follows: [{"id":"d023c5e3-ca3c-4d97-933a-1112a8516eee", "score":9001, "updated":"2018-12-07T13:48:33.6366278", "player":Johanna, "category":Funny}, {"id":"398b65fb-e741-4801-b ...

Approach to Using Bootstrap 4 with Intl-Tel-Input

I've been attempting to integrate intl-tel-input with bootstrap 4, but I'm facing an issue where the placeholder for the input is not showing up. I've tried various solutions found online, but none of them seem to work. Here's my HTML ...

Insert DOM elements at the start of the parent element

I'm currently using the following JavaScript code to insert AJAX responses into a div with an ID of results: document.getElementById("results").innerHTML=xmlhttp.responseText; The issue I am encountering is that this code adds all new elements after ...

The console is reporting an error stating it cannot read the charCodeAt property

<script> var str=prompt('please enter a string'); var a= str.split(''); for (j=0; j<str.length; j++){ for(i=j; i<str.length; i++) { if(a[i].charCodeAt(0) > a[i+1].charCodeAt(0)) { var b= a[i]; a[i]=a[i+ ...

How to correct placeholder text display in the input of a Material UI text field

Currently, I am utilizing the material ui text field component. The issue at hand is that when the text field is in focus, the placeholder shifts to the top of the field. https://i.stack.imgur.com/P5flf.png I prefer for the placeholder to remain within ...

methods for obtaining the currently selected month

After installing the npm package v-calendar@next and @popperjs/core, I am unsure of how to retrieve the value. Can someone guide me on how to display information about the currently selected value for the month? //. //. //. //. <script> export defa ...

Which specific values could cause the function to fail?

I am considering implementing the function provided below: function removeDuplicates(array){ var output=[],object={}; for(var index=0,length=array.length;index<length;index++){ object[array[index]]=0; } for(index in object){ ...

What is the best way to ensure that the maximum price is mandatory when entering a minimum price?

Essentially, the process works like this: if a person inputs a minimum price but forgets to input a maximum price, then presses search, it will not perform the search and a message will appear next to the max price field reminding them to enter a maximum ...

Sending a style prop to a React component

My typescript Next.js app seems to be misbehaving, or perhaps I'm just not understanding something properly. I have a component called <CluckHUD props="styles.Moon" /> that is meant to pass the theme as a CSS classname in order to c ...

Is it possible to activate the identical drop-down or popover with multiple buttons?

Is it possible to activate the same drop-down menu with multiple buttons? For example, I want a button at the top of the page labeled special menu, another in the middle also labeled special menu, and one at the bottom as well labeled special menu. When a ...

Problem encountered when Next.js and CSRF token interact on the server

I've integrated the next-csrf library (https://github.com/j0lv3r4/next-csrf) into my next.js app to safeguard api routes. Despite following the documentation, I'm encountering a 500 error with the following message: {"message":"Si ...

The function of hasClass within an if statement appears to be malfunctioning

I'm struggling to figure out why my .hasClass function is not functioning correctly. I've implemented the Quick Search jQuery plugin, and below is the code I am using: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.mi ...

What steps do I need to follow to develop a checkbox component that mirrors the value of a TextField?

I am working with 3 components - 2 textfields and 1 checkbox Material UI component. My goal is to have the checkbox checked only when there is a value in one of the textfield components. What would be the most effective way to achieve this functionality? ...

Eliminate the object's item before storing it

Attempting to simplify the explanation as much as possible, in my form there is a select dropdown where users can choose from multiple contracts. Once a contract is selected, I aim to remove certain items from that specific contract before saving it. The ...

Vue.js enhances user input interactions

CSS <span :style="{ display : displayTitle }" @click="toggleInput()"> {{ text }} </span> <input v-if="isEditing" type="text" v-model="text" @blur="hideInput" @keydown.enter="saveChanges" @keydown.esc="cancelE ...

Node.js project that was once functional is now encountering issues with the error message: "Error: Route.post() requires a callback function but received [object Undefined]."

When it comes to asking questions, I typically provide a lot more detail and use specific wording in my titles. However, I'm currently facing an issue that has left me stumped on where to find the solution. Recently, I delved into learning Node JS an ...

Conceal any zero values from an empty numerical input

Currently, I have a form that retrieves data from a database and includes a number input type field. When the field is empty, it defaults to displaying "0." However, I would like to hide the "0" and only show the value if it is different from 0. Despite a ...

In React, CSS @media queries are specifically designed to function only within the Device Mode of Developer Tools

As indicated by the title, I have designed a responsive web app using the React framework along with various @media queries. When I resize the page in Developer Tools' Device Mode, the queries work perfectly fine and everything functions as expected. ...

Child Component Unable to Access Vue Props

My root element contains logged in user data, which I am passing as props to the user profile component. However, when I attempt to access User object properties in the child component, it throws an error and shows as undefined. In my app.js: Vue.compone ...

Accessing data from Execution Contexts in JavaScript

var value = 10; var outer_funct = function(){ var value = 20; var inner_funct = function(){ var value = 30; console.log(value); // displays 30 console.log(window["outer_funct"]["value"]); // I want to log the value 20 her ...