vue2遍历数组的时候为数组动态的添加id

分别用操作dom和vue完成导航栏的例子实现遍历数组的时候动态的为数组中的元素添加id属性。

预期效果:将数组中的数据展示在页面中,当点击不同的数据的时候,更新被选中数据的样式。

        

目录

1. 用操作dom的方法实现(不推荐)

2. vue中的v-bind实现


1. 用操作dom的方法实现(不推荐)

<template>
<!-- 一级导航栏 -->
    <ul class="nav nav-pills">
      <li><a>一级:</a></li>
      <li><a href="#" @click="liClick(-1)" id = "active" class="all1">全部</a></li>
      <li v-for="(l, index) in Lists" :key="index" @click="liClick(index)">
        <a href="#" class="sele">
          {{ l.name }}
        </a>
      </li>
    </ul>
</template>

<script>
export default {
    methods: {
        
      liClick(index) {
          this.temp = index;
          document.getElementById("active").removeAttribute("id","active");
          if (index >= 0 && index < this.Lists.length) {
              document.getElementsByClassName("sele")[index].setAttribute("id","active");
              this.seen = true;
              this.SecLis = this.Lists[index].cate; 
          } else {
              document.getElementsByClassName("all1")[0].setAttribute("id","active");
              this.seen = false;
          }
       }

    },
</script>

<style scoped>
    #active{
      background-color: gainsboro;
    }
</style>

2. vue中的v-bind实现

<template>
    <ul class="nav nav-pills">
      <li><a>一级:</a></li>
      <li>
        <a href="#" @click="liClick(-1)" :id="currentIndex1 === -1 ? 'active' : ''" class="all1" >全部</a >
      </li>
      <li v-for="(l, index) in Lists" :key="index" @click="liClick(index)">
        <a href="#" class="sele" :id="currentIndex1 === index ? 'active' : ''">
          {{ l.name }}
        </a>
      </li>
    </ul>
</template>

<script>
export default {
    data() {
        return {
            currentIndex1: -1,
        };
    },

    methods: {
        liClick(index) {
            this.currentIndex1 = index;
            if (index >= 0) {
                this.seen = true;
                this.SecLis = this.Lists[index].cate;
            }
        },
    },
}
</script>

<style scoped>
    #active {
        background-color: gainsboro;
    }
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Vue中,可以使用一些方法来实现对象数组中相同元素的去重。 方法一:使用Set 可以利用Set数据结构的特性来去除数组中重复的元素。首先将数组转化为Set,然后再将Set转化为数组。 ```javascript // 假设需要去重的数组为arr const set = new Set(arr); // 将数组转化为Set const newArr = Array.from(set); // 将Set转化为数组 ``` 方法二:使用filter 利用filter方法和indexOf判断元素是否已经存在于新数组中,如果不存在则添加到新数组中。 ```javascript // 假设需要去重的数组为arr const newArr = arr.filter((item, index, array) => { return array.findIndex((j) => j.id === item.id) === index; }); ``` 方法三:使用reduce 使用reduce方法遍历原始数组,将未出现过的元素添加到新数组中。 ```javascript // 假设需要去重的数组为arr const newArr = arr.reduce((prev, cur) => { if (!prev.find((item) => item.id === cur.id)) { prev.push(cur); } return prev; }, []); ``` 以上是三种常见的方法来实现Vue对象数组的去重。根据具体需求和数据结构的复杂程度,选择适合的方法来进行去重。 ### 回答2: 在Vue中,可以使用JavaScript的数组去重方法来去除相同元素。下面是一种简单的方法来实现这个功能: 1. 首先,定义一个Vue对象数组,包含了一些相同的元素: ``` data() { return { items: [ { id: 1, name: 'apple' }, { id: 2, name: 'banana' }, { id: 1, name: 'apple' }, { id: 3, name: 'orange' }, { id: 2, name: 'banana' }, ] } } ``` 2. 然后,在Vue的计算属性中使用数组的`reduce`方法来去重: ``` computed: { uniqueItems() { const uniqueArray = this.items.reduce((distinctArray, currentItem) => { // 判断当前元素在去重后的数组中是否存在,若不存在则添加到新数组中 if (!distinctArray.some(item => item.id === currentItem.id && item.name === currentItem.name)) { distinctArray.push(currentItem); } return distinctArray; }, []); return uniqueArray; } } ``` 3. 最后,在Vue的模板中使用`uniqueItems`来显示去重后的数组: ``` <div v-for="item in uniqueItems" :key="item.id">{{ item.name }}</div> ``` 以上代码会将相同`id`和`name`的元素去重,并显示在页面上。 ### 回答3: 要对Vue对象数组进行去重,可以按照以下步骤进行操作: 首先,创建一个新的空数组用于存放去重后的元素。 然后,遍历原数组中的每个元素,对于数组中的每个对象,使用JSON.stringify()方法将其转化为字符串形式。 接下来,检查新数组中是否已经存在该字符串形式的元素。如果不存在,则将该字符串形式的元素添加到新数组中。 最后,将新数组中的元素重新转化为对象形式,并存储在Vue的data属性中。 具体实现如下: ```javascript // 假设原数组vueArray,新数组为newArray let newArray = []; for (let i = 0; i < vueArray.length; i++) { let stringifiedElement = JSON.stringify(vueArray[i]); if (!newArray.includes(stringifiedElement)) { newArray.push(stringifiedElement); } } // 将新数组中的元素重新转化为对象形式,存储在Vue的data属性中 this.$data.array = newArray.map(element => JSON.parse(element)); ``` 以上代码将实现对Vue对象数组元素的去重。需要注意的是,这种方法对于对象数组中的函数属性是无法完全去重的,因为JSON.stringify()方法无法将函数属性转化成字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值