angularjs - Convert json object in to array to iterate through ng-repeat using Javascript? -
this question has answer here:
here sample json , getting json obj firebase have convert list in array bind in html trough ng-repeat.
my json object is
{ "cats1": { "name": "cricket", "imgurl": "some url", "list1": { "bat": { "name": "bat", "imgurl": "some url", "price": "$100" }, "pads": { "displayname": "pads", "imgurl": "some url", "price": "$50" } } }, "cats2": { "name": "football", "imgurl": "some url" } }
how required
this array structure required , when add new list must store uniquely in cricket category.
[ { "name": "cricket", "imgurl": "some url", "list1": [ { "name": "bat", "imgurl": "some url", "price": "$100" }, { "displayname": "pads", "imgurl": "some url", "price": "$50" } ] }, { "name": "football", "imgurl": "some url" } ]
i new angular 1 please me figure out problem
use object.keys
, pass them on array.prototype.map
create array want - see demo below:
var object={cats1:{name:"cricket",imgurl:"some url",list1:{bat:{name:"bat",imgurl:"some url",price:"$100"},pads:{displayname:"pads",imgurl:"some url",price:"$50"}}},cats2:{name:"football",imgurl:"some url"}}; var result = object.keys(object).map(e=>object[e]); console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
edit:
correcting solution make list1
array:
var object={cats1:{name:"cricket",imgurl:"some url",list1:{bat:{name:"bat",imgurl:"some url",price:"$100"},pads:{displayname:"pads",imgurl:"some url",price:"$50"}}},cats2:{name:"football",imgurl:"some url"}}; var result = object.keys(object).map(function(e){ object.keys(object[e]).foreach(function(k){ if(typeof object[e][k] == "object") { object[e][k] = object.keys(object[e][k]).map(function(l){ return object[e][k][l]; }); } }); return object[e]; }); console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
Comments
Post a Comment