admin管理员组文章数量:1415467
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]
本文标签: javascriptHow do I create array from a single element in react nativeStack Overflow
版权声明:本文标题:javascript - How do I create array from a single element in react native? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745181147a2646471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论