i can't figure out how flat collection of products i've managed data in form prods/$companykeys/$prodkeys
{ "id1" : { "prodid1" : { "name" : "car" }, "prodid2" : { "name" : "door" }, "prodid3" : { "name" : "sandwich" } }, "id2" : { "prodid4" : { "name" : "glass" }, "prodid5" : { "name" : "pen" } } }
is there simple way in rxjs or in angularfire2 directly collection of products, omitting company ids? or have re-arrange data?
thanks
observable.flatmap
made such purpose (fiddle):
// turn products array var arr = []; for(var key in products) { arr.push(products[key]); } rx.observable.from(arr) .flatmap(x => { // 'x' object id1, create array of products var arr = []; (var key in x) { arr.push(x[key]); } return rx.observable.from(arr); }) .subscribe(x => document.write(json.stringify(x) + '</br>'));
output:
{"name":"car"} {"name":"door"} {"name":"glass"} {"name":"sandwich"} {"name":"pen"}
not sure how want handle keys, can use observable.pairs
make easier loop through object properties. resulting stream of arrays first element being key , second being value object properties:
rx.observable.pairs(products) .flatmap(x => rx.observable.pairs(x[1])) .subscribe( x => console.log(x[0], x[1]));
this returns arrays this:
["prodid1", {name: "car"}] ["prodid2", {name: "door"}] ["prodid3", {name: "glass"}] ["prodid4", {name: "sandwich"}] ["prodid5", {name: "pen"}]
Comments
Post a Comment