简体   繁体   English

Vue JS - 删除 vue 组件中的重复数据

[英]Vue JS - remove repeating data in the vue component

I have lots of results that display from a component.我有很多从组件显示的结果。 Im having problems with the location data which I want to display in the component.我想在组件中显示的位置数据有问题。 Its sitting as multiple elements with |它作为多个元素与 | to separate them.将它们分开。 How do I filter down to remove all duplicates in each result and display it as a comma separated list.如何过滤以删除每个结果中的所有重复项并将其显示为逗号分隔列表。

I would like the data to appear from the component我希望数据从组件中显示

Title: Holiday One Location:AZ,FL,TX,MA,AZ,MS,TN,AL标题:假日一地点:AZ,FL,TX,MA,AZ,MS,TN,AL

Title: Holiday two Location:TN,AZ,FL,AL标题:假期二地点:TN,AZ,FL,AL

Title: Holiday three Location:AZ,NY,WY,TN,FL, AL标题:假期三地点:AZ,NY,WY,TN,FL,AL

The main page主页

<result
 v-for="(result, index) in results"
 :result="result"
 :index="index"              
 />

This is the result component.这是结果组件。

<template>
  <div>
    <div v-if="location">{{ location }}
 </div>

<script>
export default {
  name: "result",
  
  props: {
    result: Object,
  },
  data: function () {
    return {
      location: this.result.location,
  </script>   

JSON JSON

"results": {
"title": "Holiday one",
"location": "AZ|AZ|FL|TX",
"location": "MA|AZ|MS|TX",
"location": "TN|AZ|FL|AL",
"date": "22/08/2022",
},
"results": {
"title": "Holiday two",
"location": "TN|AZ|FL|AL",
"date": "20/08/2022",
},
"results": {
"title": "Holiday three",
"location": "AZ|NY|NY|WY",
 "location": "TN|AZ|FL|AL",
"date": "10/08/2022",
},
"results": {
"title": "Holiday four",
"location": "MA|AZ|MS|TX",
"location": "TN", 
 "location": "CN", 
"date": "14/08/2022",
},
"results": {
"title": "Holiday five",
"location": "CL|VT|NB|TX",
"location": "RI|NH ",
 "location": "TX",
 "location": "SC|NC",
"date": "18/08/2022",
},

Any help appreciated任何帮助表示赞赏

  1. Convert string to array.将字符串转换为数组。 Use String.prototype.split() to create an array from a string using '|'使用String.prototype.split()使用 '|' 从字符串创建数组as the delimiter.作为分隔符。

  2. Remove duplicates.删除重复项。 There are many ways you can choose to remove duplicates, one easy way is to convert an array to a Set which can only contain unique values, then convert back to an array.有很多方法可以选择删除重复项,一种简单的方法是将数组转换为只能包含唯一值的Set ,然后再转换回数组。

  3. Convert back into comma separated string.转换回逗号分隔的字符串。 Array.prototype.join() will convert an array to a string using whatever delimiter you'd like. Array.prototype.join()将使用您喜欢的任何分隔符将数组转换为字符串。

A one liner of all three steps would look like this:所有三个步骤的一个衬里看起来像这样:

const nonDuplicateLocations = Array.from(new Set(results.location.split('|'))).join(',')

First of all, your JSON is invalid.首先,你的 JSON 是无效的。 You can't specify a key more than once on the same object.您不能在同一个 object 上多次指定一个键。 You'll get an error.你会得到一个错误。 Since you seem to have multiple holidays, you might want to use an array, not an object.由于您似乎有多个假期,您可能想要使用数组,而不是 object。
For the same reason, inside each Holiday, you might want to use a locations array.出于同样的原因,在每个假期中,您可能想要使用locations数组。

So your data should probably look like this:所以你的数据应该是这样的:

{
  "results": [
    {
      "title": "Holiday one",
      "locations": ["AZ|AZ|FL|TX", "MA|AZ|MS|TX", "TN|AZ|FL|AL"],
      "date": "22/08/2022"
    },
    {
      "title": "Holiday two",
      "locations": ["TN|AZ|FL|AL"],
      "date": "20/08/2022"
    },
    {
      "title": "Holiday three",
      "locations": ["AZ|NY|NY|WY", "TN|AZ|FL|AL"],
      "date": "10/08/2022"
    },
    {
      "title": "Holiday four",
      "locations": ["MA|AZ|MS|TX", "TN", "CN"],
      "date": "14/08/2022"
    },
    {
      "title": "Holiday five",
      "locations": ["CL|VT|NB|TX", "RI|NH ", "TX", "SC|NC"],
      "date": "18/08/2022"
    }
  ]
}

Now, with the above data, my understanding is that you want to collect all the 2-letter codes from each location, remove duplicates and display them comma separated.现在,有了上面的数据,我的理解是你想从每个位置收集所有的 2 字母代码,删除重复项并以逗号分隔显示它们。 I'm guessing sorting wouldn't hurt, either.我猜排序也不会受到伤害。
Here's how you could do it:你可以这样做:

 const results = [ { title: "Holiday one", locations: ["AZ|AZ|FL|TX", "MA|AZ|MS|TX", "TN|AZ|FL|AL"], date: "22/08/2022", }, { title: "Holiday two", locations: ["TN|AZ|FL|AL"], date: "20/08/2022", }, { title: "Holiday three", locations: ["AZ|NY|NY|WY", "TN|AZ|FL|AL"], date: "10/08/2022", }, { title: "Holiday four", locations: ["MA|AZ|MS|TX", "TN", "CN"], date: "14/08/2022", }, { title: "Holiday five", locations: ["CL|VT|NB|TX", "RI|NH ", "TX", "SC|NC"], date: "18/08/2022", }, ]; new Vue({ el: "#app", data: () => ({ holidays: results, }), methods: { renderLocations(h) { return [...new Set(h.locations.join("|").split("|"))].sort().join(", "); }, }, });
 dl:not(:last-child) { border-bottom: 1px solid #ddd; padding-bottom: 1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.7.10/vue.min.js"></script> <div id="app"> <dl v-for="(holiday, index) in holidays":key="index"> <dt>Title</dt> <dd v-text="holiday.title"></dd> <dt>Locations</dt> <dd v-text="renderLocations(holiday)"></dd> <dt>Date</dt> <dd v-text="holiday.date"></dd> </dl> </div>

Let's take renderLocations apart:让我们把renderLocations分开:

  • takes all the location entries of holiday arg ( h.locations )获取假期 arg ( h.locations ) 的所有位置条目
    => ["AZ|AZ|FL|TX", "MA|AZ|MS|TX", "TN|AZ|FL|AL"] => ["AZ|AZ|FL|TX", "MA|AZ|MS|TX", "TN|AZ|FL|AL"]
  • glues them in a single string, using |使用|将它们粘在一个字符串中as separator ( .join('|') )作为分隔符( .join('|')
    => "AZ|AZ|FL|TX|MA|AZ|MS|TX|TN|AZ|FL|AL" => "AZ|AZ|FL|TX|MA|AZ|MS|TX|TN|AZ|FL|AL"
  • unglues the string in a strings array, using |使用|解开字符串数组中的字符串as separator ( .split('|') )作为分隔符( .split('|')
    => ["AZ", "AZ", "FL", "TX", "MA", "AZ", "MS", "TX", "TN", "AZ", "FL", "AL"] => ["AZ", "AZ", "FL", "TX", "MA", "AZ", "MS", "TX", "TN", "AZ", "FL", "AL"]
  • removes duplicates from the array - the ( [...new Set( )] ) wrapper从数组中删除重复项 - ( [...new Set( )] ) 包装器
    => ["AZ", "FL", "TX", "MA", "MS", "TN", "AL"] => ["AZ", "FL", "TX", "MA", "MS", "TN", "AL"]
  • sorts them alphabetically .sort()按字母顺序对它们进行排序.sort()
    => ["AL", "AZ", "FL", "MA", "MS", "TN", "TX"] => ["AL", "AZ", "FL", "MA", "MS", "TN", "TX"]
  • glues them into a string, using , as separator ( .join(', ') )将它们粘合成一个字符串,使用,作为分隔符( .join(', ')
    => "AL, AZ, FL, MA, MS, TN, TX" => "AL, AZ, FL, MA, MS, TN, TX"

Same example as above, using Vue 3 and a dedicated <Holiday /> component:与上面相同的示例,使用 Vue 3 和专用的<Holiday />组件:

 const results = [ { title: "Holiday one", locations: ["AZ|AZ|FL|TX", "MA|AZ|MS|TX", "TN|AZ|FL|AL"], date: "22/08/2022", }, { title: "Holiday two", locations: ["TN|AZ|FL|AL"], date: "20/08/2022", }, { title: "Holiday three", locations: ["AZ|NY|NY|WY", "TN|AZ|FL|AL"], date: "10/08/2022", }, { title: "Holiday four", locations: ["MA|AZ|MS|TX", "TN", "CN"], date: "14/08/2022", }, { title: "Holiday five", locations: ["CL|VT|NB|TX", "RI|NH ", "TX", "SC|NC"], date: "18/08/2022", }, ]; const { createApp, defineComponent } = Vue; const Holiday = defineComponent({ template: "#holiday-tpl", props: ["holiday"], setup: () => ({ renderLocations: (a) => [...new Set(a.join("|").split("|"))].sort().join(", "), }), }); const App = createApp({ components: { Holiday }, setup: () => ({ holidays: results, }), }); App.mount("#app");
 dl:not(:last-child) { border-bottom: 1px solid #ddd; padding-bottom: 1rem; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.37/vue.global.prod.min.js"></script> <div id="app"> <Holiday v-for="(holiday, index) in holidays":key="index":holiday="holiday" /> </div> <template id="holiday-tpl"> <dl> <template v-for="(value, name) in holiday"> <dt:key="`dt-${name}`" v-text="name.charAt(0).toUpperCase() + name.slice(1)" /> <dd:key="`dd-${name}`" v-text="name === 'locations'? renderLocations(value): value" /> </template> </dl> </template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM