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]
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]
- 2 That's right. Just put it into array literal and you get an array of single element. – DecPK Commented Sep 8, 2021 at 10:38
- Yes. There are few other ways too but yours is already the most concise. – Tushar Shahi 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. – Dan Zuzevich Commented Sep 8, 2021 at 10:57
2 Answers
Reset to default 3You have several ways to do this:
init an array and push obj inside:
let array = []; array.push(obj);
init an array with obj:
let array = [obj];
use
fill
function:let array = new Array(1).fill(obj);
You can try this,
const array = [];
array.push(obj);
Output:
array = [obj]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181148a4615424.html
评论列表(0条)