可以通过以下方式使用v-for和v-if判断一个数组里面是否有重复值:

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="index" v-if="isUnique(item)">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [1, 2, 3, 2, 4, 5, 4],
    };
  },
  methods: {
    isUnique(value) {
      return this.items.filter((item) => item === value).length === 1;
    },
  },
};
</script>

在上面的代码中,我们使用了v-for来遍历items数组,并使用v-if指令来判断当前的item是否唯一。通过调用isUnique方法,我们使用Array的filter方法来筛选出与当前item相同的元素,并返回一个新的数组。然后我们判断这个新数组的长度是否为1,如果是1则表示当前item是唯一的,我们就展示它,否则不展示它

vuejs怎么通过v-for和v-if判断一个数组里面是否有重复值

原文地址: https://www.cveoy.top/t/topic/fG0X 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录