Here’s a 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 for each element in the array, obj = element at current index
result[obj.Room] = {
9: obj[9],
10: obj[10]
};
});
console.log(result);
The basics are: [] is an array; {} is an object. In arrays things are identified by their index(0,1,2…). In objects they have keys. So
obj = {"201": {'key1':'value1', 'key2': 'value2'}}
means you have an object inside which you have another nested object (identified by it’s key – 201). The nested object has 2 properties. The first property has a key(key1) and a value (value1)
obj["201"] is {'key1':'value1', 'key2': 'value2'}
EDIT:
I suppose you want a more general solution
var newObj ={};
Object.keys(obj).forEach(function(key) {
if(key !== 'Room') {
newObj[key] = obj[key];
}
});
result[obj.Room] = newObj;
See if you can figure out where to put this
3
solved How Can i Change the style of my angularjs object?