const arr = [
{
id: '0'
},
{
id: '0.0'
},
{
id: '0.1'
},
{
id: '0.2'
},
{
id: '0.3'
},
{
id: '1'
},
{
id: '1.0'
},
{
id: '1.1'
},
{
id: '2'
},
{
id: '2.0'
},
{
id: '2.1'
},
{
id: '2.2'
},
...
]
const result0 = [
{
id: '0'
},
{
id: '0.0'
},
{
id: '0.1'
},
{
id: '0.2'
},
{
id: '0.3'
}
]
const result1 = [
{
id: '1'
},
{
id: '1.0'
},
{
id: '1.1'
}
]
const result2 = [
{
id: '2'
},
{
id: '2.0'
},
{
id: '2.1'
},
{
id: '2.2'
},
]
const resultN = [
...
]
const results = Object.values(arr.reduce((acc, c) => {
const prefix = c.id.split('.')[0];
acc[prefix] = [...acc[prefix] ?? [], c];
return acc;
}, {})
);
/* [
[{"id":"0"},{"id":"0.0"},{"id":"0.1"},{"id":"0.2"},{"id":"0.3"}],
[{"id":"1"},{"id":"1.0"},{"id":"1.1"}],
[{"id":"2"},{"id":"2.0"},{"id":"2.1"},{"id":"2.2"}],
] */