My object looks like this { {},{},{},{} }
I want it to look like this:
[ {},{},{},{} ]
My object looks like this { {},{},{},{} }
I want it to look like this:
[ {},{},{},{} ]
IF you are on Chrome or Firefox only, you can use Object.values()
:
var o = {a:{k:1}, b:{k:2}, c:{k:3}}
var values = Object.values(o);
// [{"k":1}, {"k":2}, {"k":3}]
Otherwise, use (and accept) an answer based on Object.keys()
.
Well, first of all there is no such thing as an object of objects unless you mean an object which properties are objects. Example: { foo: { bar: 'xyz'} }
.
To convert such object into a collection (array of objects) just loop through the keys like such
let objOfObjs = {
foo: { xyz: 'bar'},
bar: { abc: 'foo'}
}, collection = [];
Object.keys(objOfObjs).forEach(key => collection.push(objOfObjs[key]));
console.log(collection); //[ { xyz: 'bar' }, { abc: 'foo' } ]
Repl: https://repl.it/I4MS
var obj = { 'a' : {}, 'b': {}, 'c': {}, 'd': {} }
var list = [];
Object.keys(obj).forEach(function(key) {
list.push(obj[key]);
});
or simpler
var list = Object.values(obj);
If you're using jQuery, you can use the $.each() method to loop through the original object.
Otherwise, you can try a for loop:
var originalObject = {
"key1": { "obj1Key": "obj1Value" },
"key2": { "obj2Key": "obj2Value" },
"key3": { "obj3Key": "obj3Value" }
}
var newArray = [];
for (var key in originalObject) {
newArray.push(originalObject[key]);
}
console.log("New Array: " + newArray);