[vue] 简单的数组对象去重

一、点击黄色背景文字,然后控制台(F12)看效果,每个只可以添加一次。

在这里插入图片描述
在这里插入图片描述

二、全部html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  </head>
  <body>
    <div id="app">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="date" label="日期"> </el-table-column>
        <el-table-column label="配送信息">
          <el-table-column prop="name" label="姓名">
            <template slot-scope="scope">
              <div
                style="background-color: yellow"
                @click="onRowClick('配送信息-姓名', 'name',scope.row.id)"
              >
                {{scope.row.name}}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="province" label="省份">
            <template slot-scope="scope">
              <div
                style="background-color: yellow"
                @click="onRowClick('配送信息-省份', 'province',scope.row.id)"
              >
                {{scope.row.province}}
              </div>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="地址">
          <el-table-column prop="city" label="市区">
            <template slot-scope="scope">
              <div
                style="background-color: yellow"
                @click="onRowClick('地址-市区', 'city',scope.row.id)"
              >
                {{scope.row.city}}
              </div>
            </template></el-table-column
          >
          <el-table-column prop="address" label="地址"
            ><template slot-scope="scope">
              <div
                style="background-color: yellow"
                @click="onRowClick('地址-地址', 'address',scope.row.id)"
              >
                {{scope.row.address}}
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <script>
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            tableData: [
              {
                date: "2016-05-03",
                name: "王小虎",
                province: "上海",
                city: "普陀区",
                address: "上海市普陀区金沙江路 1518 弄",
                id: 11,
              },
              {
                date: "2016-05-02",
                name: "王小虎",
                province: "上海",
                city: "普陀区",
                address: "上海市普陀区金沙江路 1518 弄",
                id: 22,
              },
              {
                date: "2016-05-04",
                name: "王小虎",
                province: "上海",
                city: "普陀区",
                address: "上海市普陀区金沙江路 1518 弄",
                id: 33,
              },
              {
                date: "2016-05-01",
                name: "王小虎",
                province: "上海",
                city: "普陀区",
                address: "上海市普陀区金沙江路 1518 弄",
                id: 44,
              },
              {
                date: "2016-05-08",
                name: "王小虎",
                province: "上海",
                city: "普陀区",
                address: "上海市普陀区金沙江路 1518 弄",
                id: 55,
              },
            ],
            list: [],
          };
        },
        methods: {
          // 数组对象去重
          onRowClick(label, value, id) {
            /*方法一:*/
            for (let i in this.list) {
              if (this.list[i].value === value && this.list[i].id === id) {
                return false;
              }
            }

            /*方法二:*/
            // const some = this.list.some(
            //   (v) => v.value === value && v.id === id
            // );
            // if (some === true) return false;

            /*方法三:*/
            // const filter = this.list.filter(
            //   (v) => v.value === value && v.id === id
            // );
            // if (filter.length > 0) return false;
            this.list.push({ label, value, id });
            console.log(this.list);
          },
        },
      });
    </script>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值