I’ve got data like this
[
[ '@test','1.2.6-unstable' ],
[ '@test','1.3.2-unstable' ],
[ '@test','1.4.6-unstable' ],
[ '@test2','4.0.1-unstable' ],
[ '@test2','4.0.2-unstable' ],
[ '@test2','4.0.3-unstable' ],
[ '@test3','2.2.0-unstable' ],
[ '@test3','2.2.3-unstable' ],
[ '@test3','2.2.9-unstable' ],
...
]
and I’m trying to group them by name @test
then loop round the values and apply an action on each, ignoring the first value.
My output should be
[
[ '@test','1.3.2-unstable' ],
[ '@test','1.4.6-unstable' ]
]
[
[ '@test2','4.0.2-unstable' ],
[ '@test2','4.0.3-unstable' ]
]
[
[ '@test3','2.2.3-unstable' ],
[ '@test3','2.2.9-unstable' ]
]
I have looked at these questions and I’m unable to apply the right combination to get what I need
break array of objects into separate arrays based on a property
Can't loop over grouped array in JS
JS loop through array and group with map
I’ve also tried .shift()
and .slice(1)
but this only takes the 1st one off the whole original array
My code looks like this:
const response = await axios.get(reportURL, { headers });
const mystuff = response.data.value.reduce((acc, next) => {
acc.push(...next.versions.map((v) => [next.name, v.version]));
return acc;
}, []);
const filteredArray = mystuff.filter(([_, version]) => version.includes('-unstable'));
const map = filteredArray.reduce((acc, { name, version }) => {
if (acc.has(name)) {
acc.get(name).versions.push(version);
} else {
acc.set(name, { name, versions: [version] });
}
return acc;
}, new Map());
const result = [...map.values()];
// Loop round the array and fire off the axios
result.forEach(([name, version]) => {
const URL = `https:URL/${name}/versions/${version}?api-version=7.1-preview.1`;
2
Answers
You could group by the first item of the nested arrays with
Object.groupBy
and take only the values.Finally remove the first item of each group result.
I add sample code below