传播和休息运算符

传播和休息运算符

从现有数组形成一个新数组,字符噪音更少。spread 将元素提取为值。它是一个不可变的函数。[] 是写入新数组的方式。因此,展开不会改变原始数组。spread 适用于所有可迭代对象,而不仅仅是数组。iterables:字符串、数组、映射、集合,即除了对象数据类型之外的大多数内置数据结构。 与解构的区别:从数组中提取所有元素,并且不创建新变量,仅在需要值 csv 的地方使用。当我们构建数组或将值传递给函数时使用。

const nums = [5,6,7];const newnums = [1,2, nums[0],nums[1],nums[2]];console.log(newnums); // [ 1, 2, 5, 6, 7 ]is reduced to const nums = [5,6,7];const newnums = [1,2, ...nums];console.log(newnums); // [ 1, 2, 5, 6, 7 ]console.log(...newnums); // 1 2 5 6 7

1. 将两个数组合并在一起

const arr1 = [1,2,3,4,5];const arr2 = [6,7,8,9];let nums = [...arr1,...arr2];nums; // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]const firstname = "peter";const fullname = [...firstname,' ',"p."];fullname; // [ 'p', 'e', 't', 'e', 'r', ' ', 'p.' ]console.log(...firstname); // 'p' 'e' 't' 'e' 'r'

错误来源:扩展运算符在模板字符串中不起作用,因为模板字符串不希望其中有多个值。

2. 创建数组的浅拷贝

const girl = {  name: 'melania',  friends: ['alina', 'alice', 'ayesha', 'anamika', 'anaya']};const frnz = [...girl.friends];console.log(frnz); // [ 'alina', 'alice', 'ayesha', 'anamika', 'anaya' ]console.log(girl.friends); // [ 'alina', 'alice', 'ayesha', 'anamika', 'anaya' ]

spread 也适用于对象,即使它们不是可迭代的。

let male = {  "firstname": "gangadhar",  "lastname": "shaktimaan"}let female = {  "firstname": "geeta",  "lastname": "vishwas"}let x = {...male, born: 1950};let y = {...female, born: 1940};x; // { firstname: 'gangadhar',   lastname: 'shaktimaan',   born: 1950 }y; // { firstname: 'geeta',  lastname: 'vishwas',  born: 1940 }```## shallow copy of objects:let male = {  "firstname": "gangadhar",  "lastname": "shaktimaan"}let character = {...male, firstname: 'wonderwoman'};male;         // { firstname: 'gangadhar', lastname: 'shaktimaan' }character;    // { firstname: 'wonderwoman', lastname: 'shaktimaan' }- first name for character object is changed although it was extracted from male object

休息模式和休息参数:

rest 的作用与 spread 相反,但具有与 spread 相同的语法。spread 用于构建新数组或将值传递给 fn。在这两种情况下,扩展都用于扩展元素。rest 使用相同的语法,但将这些值压缩为一个spread 用于从数组中解包元素,rest 用于将元素打包到数组中。““

展开运算符和剩余运算符的语法差异:
扩展运算符 => … 用于赋值运算符的 rhs。
const nums = [9,4, …[2,7,1]];

剩余运算符=> …将位于带有解构的赋值运算符的lhs上
const [x,y,…z] = [9,4, 2,7,1];

## rest syntax collects all the elements after the last elements into an array. doesn't include any skipped elements. - hence, it should be the last element in the destructuring assignment.- there can be only one rest in any destructuring assignment.

let diet = [‘披萨’, ‘汉堡’,’面条’,’烤’,’寿司’,’dosa’,’uttapam’];

让[第一,第三,…其他] =饮食;
首先;
第三;
其他;

- rest also works with objects. only difference is that elements will be collected into a new object instead of an arrray.

let days = { ‘mon’:1,’tue’:2,’wed’:3,’thu’:4,’fri’:5,’sat’:6,’sun’:7};
让{周六,周日,…工作} = 天;
放开= {周六,周日};

工作; // { 周一:1,周二:2,周三:3,周四:4,周五:5 }
离开; // { 周六: 6, 周日: 7 }

- although both look same, but they work differently based on where they are used.

休息和传播的微妙区别:

扩展运算符用于我们写入用逗号分隔的值使用休息模式,我们将编写用逗号分隔的变量名称。


以上就是传播和休息运算符的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1490753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月19日 13:25:16
下一篇 2025年12月19日 13:25:27

相关推荐

发表回复

登录后才能评论
关注微信