vue js从过滤的对象数组中删除重复值

xriantvc  于 2022-12-27  发布在  Vue.js
关注(0)|答案(2)|浏览(212)

我想从vue js的v-for循环中删除重复数据。我在一个数组中有一些客户端,在另一个数组中有一些类别。我正在根据clientId过滤类别,但它也显示了重复值-

    • 请从以下代码段中选择任意客户端。**
var app = new Vue({
  el: "#app",
  data() {
    return {
      clientId: 0,
      clients: [{
          "id": 1,
          "clientName": "Rafael Ellison"
        },
        {
          "id": 2,
          "clientName": "Tad Beasley"
        },
        
      ],
      categories: [{
          "clientId": 1,
          "purchaseType": "Purchase Type  1"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type  1"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 2,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 2,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 1,
          "purchaseType": "In veritatis anim al"
        }
      ],
    }
  },
  computed: {
    filteredPurchase() {
      return this.categories.filter(
        (client) => client.clientId == this.clientId
      );
    },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

  <div>
    <div>
      <label>Under Client</label>
      <select v-model="clientId">
        <option value="" selected>select clients</option>
        <option v-for="client in clients" :key="client.id" :value="client.id">{{client.clientName}}</option>
      </select>

    </div>

    <div>
      <label for="purchaseCategoryId">Purchase Type</label>
      <div class="input-group">
        <select multiple>
          <option value="" selected>select purchase Type</option>
          <option v-for="purchase in filteredPurchase" :key="purchase.id" :value="purchase.purchaseType">{{purchase.purchaseType}}</option>
        </select>
      </div>
    </div>
  </div>

</div>

有许多采购类型具有相同的名称和不同的客户端ID。我希望从采购类型中删除重复的值。

pprl5pva

pprl5pva1#

可以创建计算特性并删除重复项:

var app = new Vue({
  el: "#app",
  data() {
    return {
      clientId: 0,
      clients: [{"id": 1, "clientName": "Rafael Ellison"},
        {"id": 2, "clientName": "Tad Beasley"}, 
      ],
      categories: [{"clientId": 1, "purchaseType": "Purchase Type  1"},
        {"clientId": 1, "purchaseType": "Purchase Type  1"},
        {"clientId": 1, "purchaseType": "Purchase Type 2"},
        {"clientId": 1, "purchaseType": "Purchase Type 2"},
        {"clientId": 2, "purchaseType": "Purchase Type 2"},
        {"clientId": 1, "purchaseType": "Purchase Type 3"},
        {"clientId": 1, "purchaseType": "Purchase Type 3"},
        {"clientId": 2, "purchaseType": "Purchase Type 3"},
        {"clientId": 1, "purchaseType": "In veritatis anim al"}
      ],
    }
  },
  computed: {
    unique() {
      return [...new Map(this.categories.map(v => [JSON.stringify(v), v])).values()]
    },
    filteredPurchase() {
      return this.unique.filter(
        (client) => client.clientId == this.clientId
      );
    },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

  <div>
    <div>
      <label>Under Client</label>
      <select v-model="clientId">
        <option value="" selected>select clients</option>
        <option v-for="client in clients" :key="client.id" :value="client.id">{{client.clientName}}</option>
      </select>

    </div>

    <div>
      <label for="purchaseCategoryId">Purchase Type</label>
      <div class="input-group">
        <select multiple>
          <option value="" selected>select purchase Type</option>
          <option v-for="purchase in filteredPurchase" :key="purchase.id" :value="purchase.purchaseType">{{purchase.purchaseType}}</option>
        </select>
      </div>
    </div>
  </div>

</div>
kt06eoxx

kt06eoxx2#

这是一个与JS相关的问题。

  • 你可以使用Set constructor来创建集合,这是一种没有重复项的数据结构。*

我是这样修理的-
1.我使用Set operatorJSON.stringify创建了一组字符串化对象。
1.使用spread(...)运算符将集合转换回数组
1.使用JSON.parse将字符串数组转换回对象数组。
并且,相同的purchaseTypes和相同的clientId将被删除。

var app = new Vue({
  el: "#app",
  data() {
    return {
      clientId: 0,
      clients: [{
          "id": 1,
          "clientName": "Rafael Ellison"
        },
        {
          "id": 2,
          "clientName": "Tad Beasley"
        },

      ],
      categories: [{
          "clientId": 1,
          "purchaseType": "Purchase Type  1"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type  1"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 2,
          "purchaseType": "Purchase Type 2"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 1,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 2,
          "purchaseType": "Purchase Type 3"
        },
        {
          "clientId": 1,
          "purchaseType": "In veritatis anim al"
        }
      ],
    }
  },

  computed: {
    filteredPurchase() {
      let categories = this.categories.filter(
        (client) => client.clientId == this.clientId
      );
      return [...new Set(categories.map(a => JSON.stringify(a)))].map(a => JSON.parse(a))
    },
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

  <div>
    <div>
      <label>Under Client</label>
      <select v-model="clientId">
        <option value="" selected>select clients</option>
        <option v-for="client in clients" :key="client.id" :value="client.id">{{client.clientName}}</option>
      </select>

    </div>

    <div>
      <label for="purchaseCategoryId">Purchase Type</label>
      <div class="input-group">
        <select multiple>
          <option value="" selected>select purchase Type</option>
          <option v-for="purchase in filteredPurchase" :key="purchase.id" :value="purchase.purchaseType">{{purchase.purchaseType}}</option>
        </select>
      </div>
    </div>
  </div>

</div>

相关问题