How to Convert Object to array in Angular 4?

后端 未结 2 1526
醉话见心
醉话见心 2021-01-02 22:12

I want to convert my Object to array, here is my Object.

{5.0: 10, 28.0: 14, 3.0: 6}

I want array like below

[         


        
2条回答
  •  抹茶落季
    2021-01-02 23:11

    Use Object.keys and array.map:

    var obj = {5.0: 10, 28.0: 14, 3.0: 6}
    
    var arr = Object.keys(obj).map(key => ({type: key, value: obj[key]}));
    
    console.log(arr);

    And if your browser supports Object.entries, you can use it:

    var obj = {5.0: 10, 28.0: 14, 3.0: 6}
    
    var arr = Object.entries(obj).map(([type, value]) => ({type, value}));
    
    console.log(arr);

提交回复
热议问题