I have this array :
array=[{x:2,y:test0,z:title},{x:3,y:test0,z:title},{x:4,y:test0,z:title},{x:5,y:test0,z:title2},{x:6,y:test0,z:title2},{x:7,y:test0,z:title2}]
And I turned it into this array this way:
const groups = array.reduce((acc, row) => {
acc[row[title]] = acc[row[title]] || [];
acc[row[title]].push(row);
return acc;
}, {});
this is result:
[
title:[{x:2,y:test0},{x:3,y:test0},{x:4,y:test0}]
title2:[{x:5,y:test1},{x:6,y:test1},{x:7,y:test1}]
]
And I want to have this array:
[
title:[test0:[{x:2},{x3:},{x:4}]]
title2:[test1:[{x:5}],test2:[{x:6}],test3:[{x:7}]]]
]
How can I get to this array?
You want y to be the key. You can just use object destructuring like:
const {y, ...obj} = row[title];
const fixedRow = {[y]: obj};
In this way y gets removed from the object, and you can use the previous value of y as key and the remaining properties as value.
This won't look exactly like your expected result, because arrays can't have properties, but I suppose it's just a typo.