[英]How can I get the map function to return an array which is greater than the first?
from the Array.prototype.map doucmentation , it mentions that it should be possible to mutate the array, from which map was called upon, from within the map callback, which takes three parameters, #1 value, #2 index, #3 the original array, in addition to the following:从Array.prototype.map 文档中,它提到应该可以从 map 回调中对调用 map 的数组进行变异,它采用三个参数,#1 值,#2 索引,#3原始数组,除此之外:
thisArgOptional Value to use as this when executing callback.
thisArgOptional 执行回调时用作 this 的值。 ... Parameters in Detail
... 详细参数
callback is invoked with three arguments: the value of the element, the index of the element, and the array object being mapped.
callback 被三个 arguments 调用:元素的值,元素的索引,和被映射的数组 object。
If a thisArg parameter is provided, it will be used as callback's this value.
如果提供了 thisArg 参数,它将用作回调的 this 值。 Otherwise, the value undefined will be used as its this value.
否则,值 undefined 将用作它的 this 值。 The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function.
回调最终可观察到的 this 值是根据确定 function 所见 this 的通常规则确定的。
map does not mutate the array on which it is called (although callback, if invoked, may do so).
map 不会改变调用它的数组(尽管回调,如果被调用,可能会这样做)。
The range of elements processed by map is set before the first invocation of callback.
map 处理的元素范围是在第一次调用回调之前设置的。 Elements which are appended to the array after the call to map begins will not be visited by callback.
在调用 map 开始后附加到数组的元素将不会被回调访问。
So it seems to say that although values "added to the original array" will not be traversed by map, but that implies that it is indeed possible to append to the original array, from within map. However, I'm not sure how to do this, as simply using the third array parameter and appending to it seems to do nothing (and if I'm storing it to an array I can't just use the original array variable name because map returns a brand new array):所以这似乎是说虽然“添加到原始数组”的值不会被 map 遍历,但这意味着确实可以从 map 到原始数组 append。但是,我不确定如何这样做,因为简单地使用第三个数组参数并附加到它似乎什么都不做(如果我将它存储到一个数组中,我不能只使用原始数组变量名,因为 map 返回一个全新的数组):
var myArr = [ 1, 2, 3, 4, 5 ].map(function(x, i, arr) { this.push(i); //references old array so does nothing arr.push(i); //seemingly it should adjust return x; }, myArr /* attempting to use "this" arg to see if it will work, although seemingly its not recognized */ ); console.log(myArr); //only returns original value
To answer your comment, .map()
is not the right tool for appending values to your array.要回答您的评论,
.map()
不是将值附加到数组的正确工具。 You should use it when you need to modify each element of the input array, for example by doubling each value:当您需要修改输入数组的每个元素时,您应该使用它,例如将每个值加倍:
var input = [1,2,3,4,5];
var myArr = input.map(function (x) {
return x * 2;
});
myArr.push(6,7); // Append your values in a separate statement.
console.log(myArr); // [2,4,6,8,10,6,7]
Then you append the new values in a separate step before or after the map, depending on what you want to achieve.然后你 append 在 map 之前或之后的单独步骤中的新值,这取决于你想要实现什么。
Your code is not behaving the way you expect.您的代码未按您预期的方式运行。
console.log(myArr)
seems like it's printing the original array but in fact it's not. console.log(myArr)
看起来像是在打印原始数组,但实际上不是。 As an example, if you remove all the other code you'll have this remaining:例如,如果您删除所有其他代码,您将剩下:
var myArr = [1,2,3,4,5].map(function (x) {
return x;
});
console.log(myArr); // [1,2,3,4,5]
You can see we are just returning x
without making any changes.您可以看到我们只是返回
x
而不做任何更改。 x
will resolve to 1
, then 2
, etc in turn. x
将依次解析为1
、 2
等。 The output of .map()
will therefore be [1,2,3,4,5]
which contains identical values to your input array but is not the exact same array, it's a copy.因此,
.map()
的 output 将是[1,2,3,4,5]
,它包含与输入数组相同的值,但不是完全相同的数组,它是一个副本。
You can prove this by moving your input array into a new variable and then comparing myArr === input
at the end:您可以通过将输入数组移动到一个新变量中然后在末尾比较
myArr === input
来证明这一点:
var input = [1,2,3,4,5];
var myArr = input.map(function (x) {
return x;
});
console.log(input); // [1,2,3,4,5]
console.log(myArr); // [1,2,3,4,5]
console.log(myArr === input); // false
To take this one step further if we take the 3rd parameter of .map()
and replace each element with double its value,, we'll see that the original input
array does get modified, and the return value from .map()
, myArr
stays the same.为了更进一步,如果我们采用
.map()
的第三个参数并将每个元素替换为其值的两倍,我们将看到原始input
数组确实被修改,并且.map()
的返回值, myArr
保持不变。
var input = [1,2,3,4,5];
var myArr = input.map(function (x, i, arr) {
arr[i] = x * 2;
return x;
});
console.log(input); // [2,4,6,8,10]
console.log(myArr); // [1,2,3,4,5]
console.log(myArr === input); // false
Just because you can modify the original array using the 3rd parameter of .map()
doesn't mean that you should.仅仅因为您可以使用
.map()
的第三个参数修改原始数组并不意味着您应该这样做。 Doing this is likely to introduce unexpected behaviour and makes it harder for future developers to reason about what the code is doing.这样做可能会引入意外行为,并使未来的开发人员更难推断代码在做什么。
It's far better for your functions to return a new variable rather than mutating the input parameters.函数返回一个新变量比改变输入参数要好得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.