Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

React ES6 扩展运算符


扩展运算符

JavaScript 扩展运算符 (...) 允许我们快速将现有数组或对象的一部分或全部复制到另一个数组或对象中。

示例

const numbersOne = [1, 2, 3];
const numbersTwo = [4, 5, 6];
const numbersCombined = [...numbersOne, ...numbersTwo];

亲自试一试 »

扩展运算符通常与解构结合使用。

示例

numbers 中的第一项和第二项分配给变量,并将其余项放入数组中

const numbers = [1, 2, 3, 4, 5, 6];

const [one, two, ...rest] = numbers;

亲自试一试 »

我们也可以在对象中使用扩展运算符

示例

合并这两个对象

const myVehicle = {
  brand: 'Ford',
  model: 'Mustang',
  color: 'red'
}

const updateMyVehicle = {
  type: 'car',
  year: 2021, 
  color: 'yellow'
}

const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}

亲自试一试 »

请注意,不匹配的属性被合并了,但匹配的属性 color 被传递的最后一个对象 updateMyVehicle 覆盖了。结果颜色现在是黄色。



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.