1

In python, if I want to create an array like [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] I only need to do [1] * 10

>>> [1] * 10
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Can I achieve the same in typescript?

3
  • 4
    const array = new Array(10).fill(1); Commented Feb 14, 2019 at 5:39
  • Can you turn it into an answer? I will upvote & accept it. Commented Feb 14, 2019 at 5:40
  • @RosdiKasim - Can you turn it into an answer? I will upvote & accept it. --from OP Commented Feb 14, 2019 at 6:01

2 Answers 2

2
const array = new Array(10).fill(1);

It actually originates from javascript, but of course, works on typescript too.

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

Comments

2

you can try out

Array.from({ length: 10}, () => 1) - with this you can provide function that can initialize your array with the function output , currently function returns 1 so it initialize array with 1.

or

new Array(10).fill(1); (Already as given in comment)

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.