Trying to transform an object of objects:
var items: {
item_a: {
state: 'item_a status'
},
item_b: {
state: 'item_b status'
}
};
into an array of objects, whilst adding a new array element to the object (the object key):
var items = [{
name: 'item_a',
state: 'item_a status'
}, {
name: 'item_b',
state: 'item_b status'
}];
My naive attempt, which works, is thus:
var arrayOfItems = [];
for(var x in items){
var itemObj = {
name: x
};
for(var y in items[x]){
itemObj[y] = items[x][y];
}
arrayOfItems.push(itemObj);
}
I'm wondering if there's a cleaner way to do this, using maybe something Underscore/LoDash?