skip to Main Content

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


  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; }
    Login or Signup to reply.
  2. I add sample code below

    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"],
      // ... more data
    ];
    
    const groupedData = new Map();
    
    data.forEach(([name, version], index) =>
      groupedData.has(name)
        ? groupedData.get(name).push([name, version])
        : groupedData.set(name, [])
    );
    
    groupedData.forEach((group) => console.log(group));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search