[英]map subarray with new values in javascript
I have an array, array = [[1,2,3], [4,5,6]]
.我有一个数组,
array = [[1,2,3], [4,5,6]]
。
I need a new array with the value [[1,2,3], [8,10,12]]
我需要一个值为
[[1,2,3], [8,10,12]]
的新数组
I tried array[1].map((el) => el*2)
, but it's not return the array which I want.我试过
array[1].map((el) => el*2)
,但它没有返回我想要的数组。
Please help me out to solve this issue.请帮我解决这个问题。
You might use a nested map, multiply by the index of the outer map you're on (plus one).您可以使用嵌套的 map,乘以您所在的外部 map 的索引(加一)。
const arr = [[1,2,3], [4,5,6]]; const result = arr.map((subarr, i) => subarr.map(val => val * (i + 1)) ); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.