How to convert JS Object to Array

前端 未结 8 1098
小鲜肉
小鲜肉 2020-12-14 00:24

I need to convert a hash map

{ 
    \"fruit\" : [\"mango\",\"orange\"],
    \"veg\"   : [\"carrot\"]
} 

to

[ 
  { \"type\         


        
8条回答
  •  悲哀的现实
    2020-12-14 01:07

    No Need of loop

    var a = { 
       "fruit" : ["mango","orange"],    
       "veg"   : ["carrot"]
    
    
    };  
    
    var b = [  
        { "type" : "fruit" , "pop" : function(){this.name = a[this.type]; delete this.pop; return this} }.pop() ,          
        { "type" : "veg" ,   "pop" : function(){this.name = a[this.type]; delete this.pop; return this} }.pop()   
    ]
    

提交回复
热议问题