Javascript Array.map 1 个元素到多个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38528473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:34:09  来源:igfitidea点击:

Array.map 1 element to multiple element

javascriptmap-function

提问by Boyang

I have [3, 16, 120]. when I do [3, 16, 120].map(mapper), I want to achieve, for example [4,5, 17,18, 121,122]i.e. each element map to n+1 and n+2. This is of course an example - what I want is to simply push multiple values from mapper function

我有[3, 16, 120]。当我这样做时[3, 16, 120].map(mapper),我想实现,例如,[4,5, 17,18, 121,122]即每个元素映射到 n+1 和 n+2。这当然是一个例子 - 我想要的是简单地从映射器函数中推送多个值

Do I have to use Array.each and push to an array, or is it possible to do it with Array.map (or other built-in api)

我是否必须使用 Array.each 并推送到数组,或者是否可以使用 Array.map (或其他内置 api)

回答by Nenad Vracar

You can use reduce()and add to array e+1, e+2of each element.

您可以使用reduce()并添加到e+1, e+2每个元素的数组中。

var ar = [3, 16, 120];

var result = ar.reduce(function(r, e) {
  r.push(e+1, e+2);
  return r;
}, []);

console.log(result)

This is ES6 version with arrow function

这是带有箭头功能的 ES6 版本

var ar = [3, 16, 120];

var result = ar.reduce((r, e) => r.push(e+1, e+2) && r, []);
console.log(result)

PS: Array.push seems to be faster and has no Maximum call stack..error, see below:

PS:Array.push 好像更快,没有Maximum call stack..错误,见下图:

a = Array(1000000).fill(1); st = Date.now(); Array.prototype.concat.apply([], a.map(function (n) { return [n+1, n+2]; })); console.log(`${Date.now() - st}ms `);
> RangeError: Maximum call stack size exceeded

a = Array(1000000).fill(1); st = Date.now(); a.reduce((r, e) => r.push(e+1, e+2) && r, []); console.log(`${Date.now() - st}ms `);
> 180ms

So .push is preferable comparing to accepted solution.

因此,与公认的解决方案相比,.push 更可取。

回答by Boyang

I come up with one myself, using spread operator.

我自己想出了一个,使用扩展运算符。

[].concat(...[3, 16, 120].map(x => [x+1, x+2]))

[].concat(...[3, 16, 120].map(x => [x+1, x+2]))

回答by melpomene

Not particularly nice, but it is a possible solution:

不是特别好,但这是一个可能的解决方案:

var arr = [3, 16, 120];

console.log([].concat.apply([], arr.map(function (n) { return [n+1, n+2]; })));

回答by Olivier Boissé

you could produce an array for each items, then concat all these arrays :

您可以为每个项目生成一个数组,然后连接所有这些数组:

[3, 16, 120].map(x => [x+1, x+2] ).reduce( (acc,val) => acc.concat(val), []);

回答by Nina Scholz

You could use Array#reducein combination with Array#concat.

您可以Array#reduceArray#concat.

console.log([3, 16, 120].reduce(function (r, a) {
    return r.concat(a + 1, a + 2);
}, []));

ES6

ES6

console.log([3, 16, 120].reduce((r, a) => r.concat(a + 1, a + 2), []));

回答by Freewalker

Immutable solution, with the spread operator:

不可变解决方案,使用扩展运算符:

[3, 16, 120].reduce((a, v) => [...a, v+1, v+2], [])

回答by jabacchetta

2019 Update

2019年更新

Use Array.prototype.flatMap(), introduced in ES10.

使用Array.prototype.flatMap(),在 ES10 中引入。

const oddNumbers = [1, 3, 5, 7, 9];
const allNumbers = oddNumbers.flatMap((number) => [number, number + 1]);
console.log(allNumbers); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

回答by eltonkamami

using Array#concatand Array#map

使用Array#concatArray#map

Array.prototype.concat.apply([], [3, 16, 120].map(x => [x+1, x+2] ));

回答by Me.Name

Just for fun, an ES6 solution with a generator:

只是为了好玩,一个带有生成器的 ES6 解决方案:

var arr = [3, 16, 120];

var [...result] = (function*() { for( i of arr){ yield ++i; yield ++i; }})();

console.log(result);

回答by HynekS

Using Array.prototype.flat():

使用 Array.prototype.flat():

const doubled = [3, 16, 120].map(item => [item + 1, item + 2]).flat();

console.log(doubled)

Fair warning – not a standard method to this date (posted 12/2018).

公平警告 – 到目前为止还不是标准方法(发布于 12/2018)。