What is the best way to eliminate duplicate values within a v-for array?

To eliminate duplicate values, I wrote the following code:

vue

<div class="col-md-6" style="float: left">
  <ul class="list-group">
    <li class="list-group-item"
      :class="{ active: index1 == currentIndex1 }"
      v-for="(member, index1) in uniqueMemName"
      v-bind:value="member.mem_name"
      :key="index1"
      @click="setActiveMember(member, index1)"
    >
      <strong style="margin-bottom: 5px"> {{member.mem_name}} </strong>
    </li>
  </ul>
</div>

vue (script)

  computed: {
    uniqueMemName() {
      return _.uniqBy(this.members, function(m) {
        return m.mem_name;
    });
  }
},

I have also installed lodash. However, an error is occurring. Can you identify the incorrect part?

If there is a different method than the one I used, please share.

Error displayed in console:

View error console

Array information for reference:

In my database, I have tables A and B. Table A contains only the mem_name column, while table B includes all columns.

Example ->

a.mem_name b.col1 b.col2
mem1 10 20
mem1 30 40
mem2 50 60

Currently, I am working on consolidating duplicate mem_names using lodash's unique functionality.

Answer №1

If you're considering using lodash solely for this purpose, it might be worth exploring a more efficient solution using modern vanilla JavaScript:

...
  computed: {
    uniqueMembers() {
      return [...new Set(this.members.map(member => member.name))]
    }
  }

Sets guarantee uniqueness, so by mapping the array and converting it to a set before converting it back to an array, we achieve a unique array.

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

Enhance Your Website with Dynamic Autocomplete Feature Using jQuery and Multiple Input Search Functionality

I am working on a form with three input fields. Here is the HTML structure: Name<input type="text" class="customer_name" name="order[contact][first_name]"/><br/> Email<input type="text" class="customer_email" name="order[contact][email]"/& ...

Effective ways to transmit a variable array from an HTML document to a PHP server

Is there a better method for transferring a variable array from HTML to PHP? I attempted to use the serialize function, but it doesn't seem to be functioning correctly. Any suggestions would be greatly appreciated. //HTML var arrayTextAreasNames = [ ...

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 ...

Embed API requests using JavaScript's Axios library to ensure the correct promise is returned

I am currently facing an issue with posting data to my API using axios. In order to make the request, I need to include a XSFR-token along with it. The technologies I am using for this are React, Redux, Thunk, and Axios. The problem lies in handling this ...

Encountering a problem while trying to pin a message on Discord

Whenever a message is pinned in discord, it causes the bot to crash with the following error (although it can recover with forever but that's beside the point). The pinned message can be of any type (regular or embed). if (!value) throw new RangeE ...

Discovering all the links present on a webpage using node.js

Currently, I am diving into node.js (webdriver) and encountering some challenges. Despite my efforts to search online for examples of the tasks I need help with, I have come up empty-handed. One example that has me stumped is how to log all links from a pa ...

What is the best way to transpile when using vue-cli-service to build a library?

Currently, I am in the process of creating a library primarily consisting of .vue components for reusability across various projects (not intended for public npm use) using vue-cli-service. Everything appears to be set up correctly, and I can confirm that ...

React does not auto-populate form data

I am working on a simple project using Next.js with a basic form. However, when I try to submit the form and log the form data in the console, it shows up as an empty object. I'm puzzled by this issue. Here is my code: import styles from &apos ...

Loading tab content on click using jQuery

These tabs have a chrome-like appearance. When the first tab (Facebook) is clicked, it shows Facebook content. Clicking on the second tab (Twitter) displays the content of the first tab. Tabs three, four, and five show their respective contents without any ...

What are some ways to track the loading progress of a JSON file retrieved through an Axios call?

To track progress accurately, I am contemplating implementing the following steps while making an axios call: Retrieve file size during the json file request Determine the percentage of downloaded file size from the network tab Currently, I only have a ...

Tips for utilizing various broadcast options to trigger Angular controllers according to the user's chosen selection

I'm currently developing an angularjs application that includes multiple date range pickers on a single web page. When a user selects a date range from one of these pickers, I need to send the selected dates to the corresponding angular controller ass ...

How can I adjust the font size of material-ui buttons while ensuring that they scale appropriately?

Having trouble adjusting the font sizes on Material-UI's RaisedButton for React and ensuring that the button scales properly along with it. <RaisedButton label={<span className="buttonText">Log in Here</span>} /> CSS: .buttonText ...

Formatting currency in Spanish using intl.NumberFormat displays inaccurate results

Based on my research, the output for es-ES and de-DE locale should be different. (I consulted with a Spanish colleague about this issue, and he mentioned that there is indeed a decimal after thousand in Spain) var number = 1000; console.log(new Intl.Numbe ...

Tips on sending multiple values to AngularJS Directive

Currently, I am in the process of creating a simple AngularJS directive. As I am still very new to AngularJS, I would appreciate it if the answers provided could be simplified! The directive I am working on is essentially a combobox. For those unfamiliar ...

Navigating using Javascript library in Angular 2 framework

I am currently utilizing Parse, an external JS library, within Angular JS 2. Nevertheless, I am encountering issues when attempting to call the function gotoMain() from within a callback function of Parse. It appears that certain elements are not being l ...

Inquiries prior to projecting rays

As I delve into the world of Interaction with Three.js, several questions arise in my mind. 1) Can you shed some light on what exactly Viewport Coordinates are? 2) In what ways do they differ from client Coordinates? 3) How did we come up with this form ...

Changing text on a button with JavaScript toggle functionality: A step-by-step guide

I am looking to implement a feature where I can hide and show overflow content in a div. When I click on the expand button, it expands the content. However, I want this button to toggle and change text as well. Thank you! function descriptionHeightMo ...

Ways to display a JSON object in CSV format

Is there a way to export a JSON object to a CSV file, where the sub-fields contain arrays of objects? I am unsure how to properly represent this embedded data in the CSV format. ...

There is no assigned value in scope for the shorthand property. You must either declare one or provide an initializer

I'm just starting out with TypeScript. Encountering the error 'No value exists in scope for the shorthand property 'firstName'. Either declare one or provide an initializer.' while using Prisma with Next.js to create a new user in ...

What is the operational mechanism behind drag and drop page builders?

I am currently delving into my inaugural MERN project, where the functionality requires specific components (such as a checkbox to-do list, images, text, etc...) that empower users to construct various pages, larger multi-checkbox aggregation lists, and be ...