javascript - How Can i Change the style of my angularjs object? -
i don't have experience work json abject because need please :( have object
var obj = {[ { 9 : "alfreds futterkiste", 10 : "berlin", room : "201" }, { 9 : "vaffeljernet", 10: "Århus", room : "204" } ] }
what need how can change the style of object way of changing
{ 201:{9:"alfreds futterkiste",10:"berlin"}, 204:{9:"vaffeljernet",10:"Århus"} }
here's plunker(just js) :http://plnkr.co/edit/h3endn8f6milhf2h1gaj?p=preview
var array = [ { 9 : "alfreds futterkiste", 10 : "berlin", room : "201" }, { 9 : "vaffeljernet", 10: "Århus", room : "204" } ]; var result = {}; array.foreach(function(obj) { //function runs once each element in array, obj = element @ current index result[obj.room] = { 9: obj[9], 10: obj[10] }; }); console.log(result);
the basics are: [] array; {} object. in arrays things identified index(0,1,2...). in objects have keys.
obj = {"201": {'key1':'value1', 'key2': 'value2'}}
means have object inside have nested object (identified it's key - 201). nested object has 2 properties. first property has key(key1) , value (value1)
obj["201"] {'key1':'value1', 'key2': 'value2'}
edit:
i suppose want more general solution
var newobj ={}; object.keys(obj).foreach(function(key) { if(key !== 'room') { newobj[key] = obj[key]; } }); result[obj.room] = newobj;
see if can figure out put this
Comments
Post a Comment