Here is some data I need to work with
[
[ '@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' ],
...
]
I want to group them by the name @test
, then iterate through the values and perform a task on each value in the new arrays, excluding the first value.
The desired 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 reviewed various resources but am struggling to find the right solution to achieve my goal
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 have also experimented with using .shift()
and .slice(1)
but they only remove the first element from the entire original array
Here is a snippet of my code:
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()];
// Iterate through the array and make requests using axios
result.forEach(([name, version]) => {
const URL = `https:URL/${name}/versions/${version}?api-version=7.1-preview.1`;