[英]Reduce array of object in JavaScript
I have an array of 100 indices 我有100个索引的数组
const randomArr = [...Array(100).keys()]
How to return 100 array like this 如何像这样返回100个数组
[{index: i}, {title: `title${i}`}]
where i
should be index of random array. 其中
i
应该是随机数组的索引。
Use Array.from()
: 使用
Array.from()
:
const result = Array.from({ length: 100 }, (_, i) => [ { index: i }, { title: `title${i}` } ]); console.log(result);
I think you mean that you want a list like 我想你的意思是你想要一个像这样的清单
[{index: i, title: `title${i}`}]
But here goes anyway. 但无论如何这里仍然存在。
Using Array.prototype.fill 使用Array.prototype.fill
const randomArr = Array(100).fill(0).map((e, i) => [{ index: i}, {title: `title${i}`}] ); console.log(randomArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.