简体   繁体   English

我有一个关于.map 的问题,将数组中的元素带入带有 javascript 的数组中

[英]I have a question about .map to bring elements in the array into an array with javascript

I am currently trying to bring the elements in an array into other arrays in the array.我目前正在尝试将数组中的元素带入数组中的其他 arrays 中。 I have now read that .map works well and have found newArrayA as an example.我现在读到.map运行良好,并以newArrayA为例。 Can't I just leave out the dots and just do it as in newArrayB ?我不能像在newArrayB中那样忽略这些点吗? Does anyone have anything else to read?有人还有什么要读的吗? I can find almost nothing on this example.在这个例子中我几乎找不到任何东西。 Is there anything that speaks against newArrayB ?有什么反对newArrayB的吗?

 let test = ['A', 'B', 'C', 'D']; // to [ [ 'A' ], [ 'B' ], [ 'C' ], [ 'D' ] ] newArrayA = test.map((x) => [...x]); newArrayB = test.map((x) => [x]); console.log(newArrayA); console.log(newArrayB);

You can see the difference by increasing the length of one of the strings - you get different results when you use the spread operator vs simply using the value at an index - just run this example:您可以通过增加其中一个字符串的长度来查看差异 - 使用扩展运算符与仅使用索引处的值时会得到不同的结果 - 只需运行以下示例:

 let test = ['ABCDE', 'B']; newArrayA = test.map((x) => [...x]); newArrayB = test.map((x) => [x]); console.log(newArrayA); console.log(newArrayB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM