Ways to display an array of objects based on their duplicate values

Struggling with the following example, seeking assistance

new Vue({
    el:"#app",
    data: {
     items: [
        { 
            type: "a", 
            Descr: "searching"
        },
        { 
            type: "a", 
            Descr: "maps"
        },
        { 
            type: "b", 
            Descr: "maps and social"
        },
         { 
            type: "b", 
            Descr: "searching"
        },
        { 
            type: "c", 
            Descr: "maps"
        },
        { 
            type: "c", 
            Descr: "maps and social"
        },
         { 
            type: "any_value", 
            Descr: "maps and social"
        }
    ]
    },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
    <div v-for="(item,i) in items" :key="i">
         <div>{{item.type}}</div>
         <div>{{item.Descr}}</div>
    </div>
</div>

Current output:

a
searching
a
maps
b
maps and social
b
searching
c
maps
c
maps and social
any_value
maps and social

Desired output:

   a   
 --------   
 * searching
 * maps
 
     b  
 --------   
 * maps and social
 * searching
 
      c  
 --------   
 * maps
 * maps and socials


     any_value  
 --------   
 * maps and social

Note : type and Descr can be any value,but i want them to list under the type like above example

Highly appreciated if it can be achieved using a single v-if property.

-------------------------------End------------------------------------------- this line is because the error saying It looks like your post is mostly code; please add some more details

Answer №1

To effectively group elements together, you can utilize the following approach:

new Vue({
    el:"#app",
    data() {
      return {
        items: [
          {type: "a", Descr: "searching"},
          {type: "a", Descr: "maps"},
          {type: "b", Descr: "maps and social"},
          {type: "b", Descr: "searching"},
          {type: "c", Descr: "maps"},
          {type: "c", Descr: "maps and social"},
          {type: "any_value", Descr: "maps and social"}
        ]
      }
    },
    computed: {
      grouped() {
        return this.items.reduce(
          (result, item) => ({
            ...result,
            [item['type']]: [
              ...(result[item['type']] || []),
              item,
            ],
          }), 
          {},
        )
      }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <ul v-for="(group, key) in grouped" :key="key">
    {{ key }}
    <li v-for="(item, i) in group" :key="i">
       {{ item.Descr }}
     </li>
  </ul>
</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

Unable to halt operation when xmlhttprequest.responseText is equal to a particular value

Currently, I am incorporating XmlHttp with Java servlets in the following manner: function btnSave_onclick(){ var xmlHttp; var responseText; if (condition){ var para= "someParamsHere"; var url = "urlHere"; if (window.XMLHttpRequest) { ...

Is there a way to access a component's props within the getServerSideProps() method?

Is there a way to pass parameters to a React component and then access the values of those parameters within the getServerSideProps function of the component? I am utilizing the next framework in React JS. <Menu name={menuName} /> In this example, ...

Aggregate data by various categories and compute a total sum

I developed a recipe creation tool using Laravel 5.2. With this application, users can include ingredients in their recipes, sometimes duplicating ingredients depending on their use. Currently, I am working on a feature that enables users to generate a P ...

Vue js external data model

As a beginner starting out with Vue, I have familiarized myself with the basics of imports and exports in ES6. However, I am seeking guidance on the most efficient way to manage data for several components in my project. I want to keep a centralized source ...

The compatibility issue between jQuery popover and AngularJS causes functionality disruptions

One clever trick with jQuery popover involves populating a hidden div with our data and instructing the clicked button to show a popover with that hidden div as its content. However, when dealing with multiple divs, we may encounter issues with classes. ...

Save canvas as an image, with the option to incorporate additional text from a textarea element on the page into

I am currently working with a canvas element using Fabric.JS. Beneath the canvas on the screen, there is a textarea within the DOM. The download button on the page successfully downloads the canvas element as an image. However, I now need to include the t ...

Variables for NPM Configuration

After researching the best way to securely store sensitive information, I decided to utilize the config package and environment variables for added security. Here is how I implemented this setup: Created a config directory containing two files: default.js ...

Divide the string into segments and set up pagination

<div><p class="pagi">page 1 content</p><p class="pagi">page 2 content</p><p class="pagi">page 3 content</p></div> I am currently facing a challenge with the string provided above. I need to create pagination ...

What is the best way to make this Typescript conditional shorter?

Trying to assign two constants based on a single condition has led me to the following options: const success = thing ? valueS1 : valueS2; const failure = thing ? valueF1 : valueF2; Alternatively, I considered: if (thing) { const success = valueS1; ...

The ThemeProvider does not automatically provide theme injections

After creating a theme using the createTheme method from @mui/material/styles, I attempted to apply this theme using ThemeProvider from the same package. This snippet showcases the dark theme that was created: export const darkTheme = createTheme({ pale ...

Developing an installation bundle for an InDesign JavaScriptscriptId

Unsure if the title is effectively conveying my query. Here's what I need help with: I possess a .jsx file that performs a specific function. My goal is to craft a setup file suitable for both Mac and PC, similar to .jar, .exe, .dmg. Upon installatio ...

What is the best way to find the product of each object in an array by the corresponding values in another array?

Searching for a solution to an issue I encountered while working on an assignment. The problem can be illustrated as follows: var arrOfObj = [{a:10 },{a:20},{a:30}, ......] var arrToMultiply = [2,4,6, .....] The expected result const result = [{a:10,resul ...

Troubleshooting module not being found with rekuire, requirish, or rfr in resolving relative require problem in nodejs

Looking to steer clear of the complicated relative path problem detailed here by opting for one of the suggested solutions. I've found three similar libraries that could help: rekuire node-rfr aka Require from Root requirish I've experimented ...

Organize the array based on the grandchild's value

Consider this scenario: I have a collection of objects, where each object contains another collection of objects. The structure is as follows: [ { "dislikes": [ { "createDate": { "date": 11, ...

Sharing configurations between a Node.js application and client-side JavaScript

I am facing an issue where I need to use a config object in both my node app and browser. Below is the path and content of the configuration file: path: [app]/public/js/config.js content: var config = { "foo": "bar", "num": 42, "list": ["a" ...

Steps for invoking a function in an HTML document from an external jQuery script

Can you please assist me with the logic and approach to execute a function in an HTML document after a function in an external jQuery file has been executed? I have two separate jQuery files. One is responsible for creating tables, while the other, timesh ...

Issue with saving state changes in Vuex in Nuxt framework

While working on my Vuex app, I encountered an issue. On one page, I am able to commit changes to the state successfully. However, on a specific page, when I try to fetch data from an API and store it in the state, it seems to get stuck in the mutation pro ...

What is the proper way to create an unordered list in vue.js?

I need assistance creating an unordered list for a series of errors I am currently printing from my console. I am utilizing vue.js and finding it challenging to grasp the concept behind implementing this feature. Below, you will find the relevant code. He ...

Utilizing the HTML button element within an ASP file combined with JavaScript can lead to the reloading of the

I am attempting to create a JavaScript function for an HTML button element. However, I have noticed that it causes the page to reload. Strangely, when I use an input with a button type, everything works perfectly. What could be causing this issue and why a ...

Transforming a JavaScript component based on classes into a TypeScript component by employing dynamic prop destructuring

My current setup involves a class based component that looks like this class ArInput extends React.Component { render() { const { shadowless, success, error } = this.props; const inputStyles = [ styles.input, !shadowless && s ...