Array.map 1 element to multiple element

前端 未结 10 696
一整个雨季
一整个雨季 2021-02-01 02:23

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

10条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 03:05

    You could use Array#reduce in combination with Array#concat.

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

    ES6

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

提交回复
热议问题