1

I think the question is pretty straightforward, but still:

I have an object obj. How do I create array from this single object? Is it just [obj]

3
  • 2
    That's right. Just put it into array literal and you get an array of single element. Commented Sep 8, 2021 at 10:38
  • Yes. There are few other ways too but yours is already the most concise. Commented Sep 8, 2021 at 10:40
  • 1
    Just a tip for next time, I wouldn't pose this questions as React Native based, this is purely a JavaScript question. Commented Sep 8, 2021 at 10:57

2 Answers 2

3

You have several ways to do this:

  1. init an array and push obj inside:

    let array = [];
    array.push(obj);
    
  2. init an array with obj:

    let array = [obj];
    
  3. use fill function:

    let array = new Array(1).fill(obj);
    
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this,

const array = [];

array.push(obj);

Output:

array = [obj]

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.