简体   繁体   English

如何让 map function 返回一个大于第一个的数组?

[英]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

Update更新

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 之前或之后的单独步骤中的新值,这取决于你想要实现什么。

Explanation解释

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将依次解析为12等。 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

Bonus points奖励积分

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.

相关问题 如何在Javascript函数中处理ArrowKeys和&lt;(大于)? 哪个事件和哪个代码(charCode或keyCode)? - How can I handle ArrowKeys and < (Greater Than) in a Javascript function? Which event and which code (charCode Or keyCode)? 如何找到大于 const M 的第一个数字? - How Can I find the first number greater than const M? 如何在地图中获取第一个数组索引的迭代? - How can I get the iteration of the first array index in a map? 如何使用箭头函数返回数组中的第一项? - How can I return the first item in an array using a arrow function? 我的 function 中缺少什么(返回一个大于其右侧元素的元素数组)? - What am I missing in my function (return an array of element that is greater than the element to its right)? 如何使map函数返回单个值而不是数组? - How do I make a map function return a single value rather than an array? 如何使用带有Object.keys函数的$ .map从给定Object.keys的数组中获取第一个值? - How can i use $.map with Object.keys function to get the first value from array given for Object.keys? 如何在剑道日期选择器上显示小于最小日期或大于最大日期的日期 - How can I show a date on a kendo datepicker which is less than min date or greater than max date 获取值大于x的数组中第一个元素的索引 - Get the index of the first element in an array with value greater than x JavaScript / ES6-如何获取第一个对象值的索引whos数组长度大于0 - javascript / es6 - how to get index of first object value whos array length is greater than 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM