You could use Object.values to just get all the values of an object. If you also want to support older browsers you could instead use Object.keys and map the values yourself.
var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);
console.log(input.filter(Boolean).map(function(item) {
return Object.values(item);
}));
You could also try this format: {630640: [{},{}], 634969: [{}]}
var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);
console.log(input.filter(Boolean).reduce(function(result, item) {
result[item.id] = Object.values(item).filter(function(property) {
return typeof property === "object" && property;
});
return result;
}, {}));
Or this format: [{id:630640, list: [{},{}]},{id:634969, list: [{}]}]
var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);
console.log(input.filter(Boolean).map(function(item) {
return {
id: item.id,
list: Object.values(item).filter(function(property) {
return typeof property === "object" && property;
})
};
}));
If you want to support older browser support you can polyfill Object.values like this:
Object.defineProperty(Object, "values", {
configurable: false,
writable: false,
value: function(obj) {
return Object.keys(obj).map(function(key) {
return obj[key];
});
}
});
Object.values?var input = [ { { id: '630640', stuff: 'name1', anotherProp: 'prop1' }, { id: '630640', stuff: 'name2', anotherProp: 'prop2' }, id: '630640' }, { { id: '694969', stuff: 'name3', anotherProp: 'prop3' }, id: '694969' } ];