简体   繁体   English

将对象转换为数组打字稿

[英]Convert Object to Array Typescript

I have an object我有一个对象

{0: "item A", 1: "item B", 2: "item C"}

How can I convert the object to become array like this如何将对象转换为这样的数组

[{0: "item A", 1: "item B", 2: "item C"}]

For now I have tried Object.keys(obj) but it returns each element in my object to array.现在我已经尝试过Object.keys(obj)但它将对象中的每个元素返回到数组。

Really needs help.真的需要帮助。 Thank you for helping感谢您的帮助

Just use bracket.只需使用支架。

 let obj = {0: "item A", 1: "item B", 2: "item C"} console.log([obj]);

Also, there's an specific Array function for this matter: Array.of :此外,还有一个针对此事的特定Array函数: Array.of

 console.log ( Array.of ( 1 ) )

This is more functional-friendly:这对功能更友好:

 const pipe = funs => x => funs.reduce( ( r, fun ) => fun ( r ), x ) const append = x => array => [ ...array, x ] const sum = values => values.reduce ( ( r, value ) => value + r ) const result = pipe ( [ Array.of, append ( 2 ), sum ] ) ( 1 ) console.log ( result )

I have an object我有一个对象

{0: "item A", 1: "item B", 2: "item C"}

How can I convert the object to become array like this我如何将对象转换成这样的数组

[{0: "item A", 1: "item B", 2: "item C"}]

For now I have tried Object.keys(obj) but it returns each element in my object to array.现在,我尝试了Object.keys(obj)但是它将对象中的每个元素返回到数组。

Really needs help.确实需要帮助。 Thank you for helping谢谢你的帮忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM