-1

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 value in the new arrays, 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
  • see also stackoverflow.com/questions/77641179/…, stackoverflow.com/questions/77652493/…... Why don't you just show us the big picture? What is the input and what ultimately needs to be done? Commented Dec 15, 2023 at 12:59
  • Sorry @gog I thought it was clear with what I put in the question. The big picture is I'm interrogating Azure Feeds to find all packages with -unstable appended, ignore the first 3 found versions then delete the rest. Using axios get and delete for this as there is no corresponding az command. Commented Dec 15, 2023 at 13:16

1 Answer 1

1

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.

const
    data = [['@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']],
    grouped = Object
        .values(Object.groupBy(data, ([first]) => first))
        .map(a => a.slice(1));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Without groupBy

const
    data = [['@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']],
    grouped = Object.values(data.reduce((r, a) => {
        if (r[a[0]]) r[a[0]].push(a);
        else r[a[0]] = [];
        return r;
    }, {}));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

5 Comments

Your code didn't remove first element of each list.
@J.Porter, right. please see edit.
Thank you @NinaScholz but this fails TypeError: Object.groupBy is not a function
@martyzee, this a new implementation, meybe you need a newer js version.
Ok fair enough. I'm locked down to versions due to being on a corporate estate but thanks anyway :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.