03.ES6数组对象展开(spread)

简介: 03.ES6数组对象展开(spread)

数组对象展开

// Create an Array
const tools = ['hammer', 'screwdriver']
const otherTools = ['wrench', 'saw']

// Unpack the tools Array into the allTools Array
const allTools = [...tools, ...otherTools]
console.log(allTools)

output:

["hammer", "screwdriver", "wrench", "saw"]

// Array of users
const users = [
  { id: 1, name: 'Ben' },
  { id: 2, name: 'Leslie' },
]
// A new user to be added
const newUser = { id: 3, name: 'Ron' }
const updatedUsers = [...users, newUser]
console.log(users)
console.log(updatedUsers)

output

[{id: 1, name: "Ben"}
 {id: 2, name: "Leslie"}]
[{id: 1, name: "Ben"}
 {id: 2, name: "Leslie"}
 {id: 3, name: "Ron"}]

浅拷贝

// Create an Array
const originalArray = ['one', 'two', 'three']
// Use spread to make a shallow copy
const secondArray = [...originalArray]
// Remove the last item of the second Array
secondArray.pop()
console.log(originalArray)

output

["one", "two", "three"]

转换可迭代对象为数组

// Create a set
const set = new Set()
set.add('octopus')
set.add('starfish')
set.add('whale')
// Convert Set to Array
const seaCreatures = [...set]
console.log(seaCreatures)

output

["octopus", "starfish", "whale"]

const string = 'hello'
const stringArray = [...string]
console.log(stringArray)

output

["h", "e", "l", "l", "o"]

对象展开

// Create an Object and a copied Object with Object.assign()
const originalObject = { enabled: true, darkMode: false }
const secondObject = Object.assign({}, originalObject)
console.log(secondObject)

等价于

// Create an object and a copied object with spread
const originalObject = { enabled: true, darkMode: false }
const secondObject = { ...originalObject }
console.log(secondObject)

output

{enabled: true, darkMode: false}

const user = {
  id: 3,
  name: 'Ron',
}
const updatedUser = { ...user, isLoggedIn: true }
console.log(updatedUser)

output

{id: 3, name: "Ron", isLoggedIn: true}

const user = {
  id: 3,
  name: 'Ron',
  organization: {
    name: 'Parks & Recreation',
    city: 'Pawnee',
  },
}
const updatedUser = { ...user, organization: { position: 'Director' } }
console.log(updatedUser)

output

id: 3
name: "Ron"
organization: {position: "Director"}

organization被覆盖

正确的做法

const updatedUser = {
  ...user,
  organization: {
    ...user.organization,
    position: 'Director',
  },
}
console.log(updatedUser)

output

id: 3
name: "Ron"
organization: {name: "Parks & Recreation", city: "Pawnee", position: "Director"}


目录
相关文章
ant-design Upload上传组件使用 编辑功能图片回显
ant-design Upload上传组件使用 编辑功能图片回显
1701 0
|
前端开发
基于jeecgboot的flowable流程任务excel导出功能
基于jeecgboot的flowable流程任务excel导出功能
522 1
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16885 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1234 2