惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
P
Proofpoint News Feed
Attack and Defense Labs
Attack and Defense Labs
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
Schneier on Security
Schneier on Security
TaoSecurity Blog
TaoSecurity Blog
H
Hacker News: Front Page
L
LangChain Blog
Y
Y Combinator Blog
T
Tenable Blog
Microsoft Security Blog
Microsoft Security Blog
L
Lohrmann on Cybersecurity
量子位
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
W
WeLiveSecurity
Martin Fowler
Martin Fowler
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
Project Zero
Project Zero
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
博客园 - 聂微东
F
Full Disclosure
WordPress大学
WordPress大学
PCI Perspectives
PCI Perspectives
P
Privacy International News Feed
M
MIT News - Artificial intelligence
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
Last Week in AI
Last Week in AI

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周 学习周刊-总第239期-2025年第48周 整理我在静态服务透明代理上的极致求索之路,最后一个你绝想不到 学习周刊-总第238期-2025年第47周 我们输了,但收获颇多 太多了,太多了 学习周刊-总第237期-2025年第46周 学习周刊-总第236期-2025年第45周 学习周刊-总第235期-2025年第44周 二五年国庆二三事 学习周刊-总第234期-2025年第43周 学习周刊-总第233期-2025年第42周 学习周刊-总第232期-2025年第41周 学习周刊-总第231期-2025年第40周 学习周刊-总第230期-2025年第39周 西湖毅行 2025年开源世界逸闻三则 学习周刊-总第229期-2025年第38周 手抄《与妻书》 CNB开发与构建基于docker-cache缓存复用的配置实践心得 学习周刊-总第228期-2025年第37周 父亲善行录 学习周刊-总第227期-2025年第36周 带你认识我之看看我的高中同学录(其二) 学习周刊-总第226期-2025年第35周 带你认识我之看看我的高中同学录(其一) 认识神级MCP工具系列--用anyquery和数据库交互 学习周刊-总第225期-2025年第34周 学习周刊-总第224期-2025年第33周 学习周刊-总第223期-2025年第32周 学习周刊-总第222期-2025年第31周 学习周刊-总第221期-2025年第30周 CNB云原生开发环境届的瑞士军刀,详解qifei项目 vuepress-vdoing主题配置自建不蒜子统计 学习周刊-总第220期-2025年第29周 写在博客发表文章1000篇的节点 从claude cli的体验聊聊最大的敌人是我们自己的成见 学习周刊-总第219期-2025年第28周 学习周刊-总第218期-2025年第27周 学习周刊-总第217期-2025年第26周 理论正确,事实错误 学习周刊-总第216期-2025年第25周 学习周刊-总第215期-2025年第24周 学习周刊-总第214期-2025年第23周 学习周刊-总第213期-2025年第22周 学习周刊-总第212期-2025年第21周 从赵心童世锦赛夺冠聊聊我的斯诺克情缘 学习周刊-总第211期-2025年第20周 记录二五年五一之短暂回归家庭 学习周刊-总第210期-2025年第19周 学习周刊-总第209期-2025年第18周 学习周刊-总第208期-2025年第17周 Go开发实践之Gin框架将前端的dist目录embed到二进制 学习周刊-总第207期-2025年第16周 近期关于cobra库的一些实践心得总结 学习周刊-总第206期-2025年第15周 我的嗜好--嗑瓜子 记大宝参加幼儿园组织的清明烈士陵园扫墓活动 学习周刊-总第205期-2025年第14周 学习周刊-总第204期-2025年第13周 学习周刊-总第203期-2025年第12周 人生实苦,何以自渡 学习周刊-总第202期-2025年第11周 近期发现的一些优秀的工具提名(一) 学习周刊-总第201期-2025年第10周 学习周刊-总第200期-2025年第09周 记一次女友喝醉使我忍术破功 爱情风波--一次分手又复合的经历 学习周刊-总第199期-2025年第08周 整理我的信息源 学习周刊-总第198期-2025年第07周
前端开发小笔记--在数组中给字段添加值与删除字段的操作
二丫讲梵 · 2025-03-26 · via 二丫讲梵

前端开发,没有基础,很多东西只能边用边琢磨边学习。学到一招,有用,赶快记录下来,作为小笔记,日积月累,成为一个辅助自己开发的手册。

那么废话不多说,直接上记录。

# 在数组中给字段添加值

看图说话:

My_Photor_1742868257404.webp

此图表单地址:点我查看 (opens new window)

# 场景概述

在一个大的表单当中,如上每行编辑可能会作为一个用户对象的子项存在,举个例子,比如这个大表单是用户的个人信息,再有一个表存用户的 VIP 充值管理,这个时候,用户的姓名就没必要在这个小表单里出现,因为大表单中已经有这个字段了。

于是小表单实际数据为:

[
    {
        "vip_type": "aiqiyi",
        "vip_pay": 10
    },
    {
        "vip_type": "youku",
        "vip_pay": 20
    }
]

1
2
3
4
5
6
7
8
9
10

但因为这张独立的表是通过用户名与用户表关联的,因此实际传递给后端的时候,需要把用户名带上。

后端希望要的表单形式如下:

[
    {
        "username": "xiaoming",
        "vip_type": "aiqiyi",
        "vip_pay": 10
    },
    {
        "username": "xiaoming",
        "vip_type": "youku",
        "vip_pay": 20
    }
]

1
2
3
4
5
6
7
8
9
10
11
12

# 解决方案

其一:在data中预先声明 username 字段后,后续赋值可直接响应式更新,无需特殊API

data() {
  return {
    tableData: [
      { vip_type: "aiqiyi", vip_pay: 10, username: '' },
      { vip_type: "youku", vip_pay: 20, username: '' }
    ]
  }
}
methods: {
  setDefaultName() {
    this.tableData.forEach(item => {
      item.username = 'xiaoming'; // 此时可直接赋值
    });
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

其二:通过Vue提供的响应式API强制触发更新机制。当通过索引操作数组元素时,$set 方法会为对象添加新属性并建立响应式追踪。(此方法我未验证)

methods: {
  addNameField() {
    this.tableData.forEach((item, index) => {
      this.$set(this.tableData[index], 'username', 'xiaoming');
    });
  }
}

1
2
3
4
5
6
7

其三:扩展运算符创建包含新字段的对象,用新数组覆盖旧数组。Vue能检测到数组引用变化并触发视图更新。

methods: {
  addNameField() {
    this.tableData = this.tableData.map(item => ({
      ...item,
      username: 'xiaoming'
    }));
  }
}

1
2
3
4
5
6
7
8

# 删除数字中的某个字段

与上边操作同样的场景,在针对列表做一些增删改查,或者拷贝操作的时候,会存在 ID 重复的情况,比如数据如下:

[
    {
        "id": 1,
        "username": "xiaoming",
        "vip_type": "aiqiyi",
        "vip_pay": 10
    },
    {
        "id": 1,
        "username": "xiaoming",
        "vip_type": "youku",
        "vip_pay": 20
    }
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14

希望把每个对象中的 ID 字段删除掉。

方案一:

methods: {
  removeNameField() {
    this.tableData.forEach(item => {
      this.$delete(item, 'id') // 删除每个对象的 id 字段
    });
  }
}

1
2
3
4
5
6
7

方案二:避免直接修改原始对象,符合 Vue 的数组变更检测机制

methods: {
  removeName(index) {
    const newItem = { ...this.tableData[index] }
    delete newItem.name // 在副本上操作
    this.tableData.splice(index, 1, newItem) // 替换原对象
  }
}

1
2
3
4
5
6
7

# el-button 增加 el-popconfirm 之后间距问题

一般我们在表格的操作区域,会有多个 button,某一些可能需要二次确认,此时借助 el-popconfirm 实现会比较好。

但是使用之后,会发现 button 之间的间距会出现问题:

My_Photor_1742871378288.webp

此时可在 delete 的 button 上增加一个 css,style="margin-left: 10px"

<el-popconfirm
	title="确定删除该记录吗?"
	confirm-button-text="确认"
	cancel-button-text="取消"
	@confirm="deleteRow(scope.$index)"
	@cancel="handleCancel">
	<template #reference>
  		<el-button
			type="danger"
			icon="el-icon-delete"
			style="margin-left: 10px"
			circle
			size="small">
		</el-button>
 	</template>
</el-popconfirm>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

然后就能解决掉 button 的边距问题了。