登录
首页 >  文章 >  前端

传播和休息运算符

来源:dev.to

时间:2024-08-24 23:45:57 321浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习文章的朋友们,也希望在阅读本文《传播和休息运算符》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新文章相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

传播和休息运算符

  • 从现有数组形成一个新数组,字符噪音更少。
  • 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.

休息和传播的微妙区别:

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

本篇关于《传播和休息运算符》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>