Maybe someone can help here.
What I want to do is:
- Build one result obj that consists of all the attributes / subobjects of all of these objects in the array
- Always have only arrays in the result obj
- Recursively build this object (as I don't know how many levels there could be and I don't know the names of the objects)
- The order of the attributes is not relevant
- Managable Except / black list for some attributes (such as id)
const arr = [{
id: 0,
nickname: 'Testnick 0',
loel: {
nice: 'like it',
},
rating: {
abc: 5,
helloworld: 2,
},
},
{
id: 1,
nickname: 'Testnick 2',
rating: {
abc: 4,
moep: 1,
},
},
{
id: 2,
nickname: 'Testnick 3',
rating: {
abc: 40,
sun: 20,
anotherObj: {
tr: 34,
subsubobj: {
sd: 24,
},
},
},
},
];
So the resultobj would look similar to this:
const result = {
id: [0, 1, 2],
nickname: ['Testnick 0', 'Testnick 2', 'Testnick 3'],
loel: {
nice: ['like it'],
},
rating: {
abc: [5, 4, 40],
helloworld: [2],
moep: [1],
sun: [20],
anotherObj: {
tr: [34],
subsubobj: {
sd: [24],
},
},
},
};
Can someone help here?
asked Nov 10, 2019 at 14:52
Gutelaunetyp
1,8824 gold badges23 silver badges43 bronze badges
-
What have you tried so far?ASDFGerte– ASDFGerte2019年11月10日 14:55:11 +00:00Commented Nov 10, 2019 at 14:55
-
Using constant values to do so, but as I don't know the amount of levels and the attribute names, need to figure out another wayGutelaunetyp– Gutelaunetyp2019年11月10日 15:10:00 +00:00Commented Nov 10, 2019 at 15:10
1 Answer 1
You coud reduce the given array and iterate the object's keys and values recursively for nested objects.
function setValues(target, source) {
Object.entries(source).forEach(([k, v]) => {
if (v && typeof v === 'object') {
setValues(target[k] = target[k] || {}, v);
} else {
target[k] = target[k] || [];
target[k].push(v);
}
});
return target;
}
var data = [{ id: 0, nickname: 'Testnick 0', loel: { nice: 'like it' }, rating: { abc: 5, helloworld: 2 } }, { id: 1, nickname: 'Testnick 2', rating: { abc: 4, moep: 1 } }, { id: 2, nickname: 'Testnick 3', rating: { abc: 40, sun: 20, anotherObj: { tr: 34, subsubobj: { sd: 24 } } } }],
result = data.reduce(setValues, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 10, 2019 at 15:13
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Gutelaunetyp
Gives me TypeError: target[k].push is not a function sometimes, need to debug. But thanks.
lang-js