2

I have an array of 100 indices

const randomArr = [...Array(100).keys()]

How to return 100 array like this

[{index: i}, {title: `title${i}`}] 

where i should be index of random array.

2
  • please add a concrete example of the wanted result. and what you have tried. Commented Mar 22, 2018 at 15:22
  • Why do you call it "random array"? Commented Mar 22, 2018 at 15:30

2 Answers 2

6

Use Array.from():

const result = Array.from({ length: 100 }, (_, i) => [
  { index: i }, { title: `title${i}` }
]);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

I think you mean that you want a list like

[{index: i, title: `title${i}`}]

But here goes anyway.

Using Array.prototype.fill

const randomArr = Array(100).fill(0).map((e, i) =>
  [{ index: i}, {title: `title${i}`}]
);

console.log(randomArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.