admin管理员组文章数量:1377530
I have an array A
:
const A = [
[1, 2],
[3, 4],
[5, 6]
];
Is there a possibility to call map
in a way that the sub arrays are expanded to named arguments of the lambda?
For instance, I want this:
const B = A.map((a, b) => a + b);
Instead of:
const B = A.map(e => e[0] + e[1]);
I have an array A
:
const A = [
[1, 2],
[3, 4],
[5, 6]
];
Is there a possibility to call map
in a way that the sub arrays are expanded to named arguments of the lambda?
For instance, I want this:
const B = A.map((a, b) => a + b);
Instead of:
const B = A.map(e => e[0] + e[1]);
Share
Improve this question
edited Mar 18 at 23:47
Spectric
32.4k6 gold badges29 silver badges54 bronze badges
asked Mar 18 at 23:41
vlad_tepeschvlad_tepesch
6,9471 gold badge44 silver badges87 bronze badges
1
|
1 Answer
Reset to default 4Sounds like a job for array destructuring:
const A = [[1,2],[3,4],[5,6]];
const B = A.map(([a,b]) => a + b);
console.log(B)
本文标签: javascriptexpand subarray to named lambda arguments in someArraymap()Stack Overflow
版权声明:本文标题:javascript - expand sub-array to named lambda arguments in someArray.map() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744487108a2608515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
map
itself do that. But it would be relatively trivial to use a different callback function, or write a wrapper tomap
which does that, or write a wrapper for the callback function. – Bergi Commented Mar 19 at 0:38