You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js:按名称分组数组并遍历忽略首个元素

问题描述

我有如下格式的数据:

[
  [ '@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' ],
  // ...更多数据
]

需要按名称(比如@test)分组,每组忽略第一个元素,只保留剩余元素,最终输出每组的结果如下:

[
  [ '@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' ]
]

我尝试过用.shift()和.slice(1),但只会移除原数组的第一个元素,没能实现分组后处理每组的需求。当前代码如下:

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`;
  // 后续请求逻辑
});
解决方案

你的代码存在两处关键问题,修正后即可实现需求:

  1. 修正分组时的解构逻辑:filteredArray中的元素是[name, version]格式的数组,不是对象,原代码中用{ name, version }解构会导致取值错误,需改为数组解构[name, version]。
  2. 分组后处理每组数据:对每个分组的版本列表使用.slice(1)跳过第一个元素,再重新映射为[name, version]的目标格式。

修正后的完整代码:

const response = await axios.get(reportURL, { headers });
// 整理成[name, version]格式的数组
const mystuff = response.data.value.reduce((acc, next) => {
  acc.push(...next.versions.map((v) => [next.name, v.version]));
  return acc;
}, []);
// 过滤出-unstable版本
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 processedGroups = [...map.values()].map(group => {
  return group.versions.slice(1).map(version => [group.name, version]);
});

// 遍历每组处理后的数据,执行axios请求
processedGroups.forEach(group => {
  group.forEach(([name, version]) => {
    const URL = `https:URL/${name}/versions/${version}?api-version=7.1-preview.1`;
    // 这里添加你的axios请求逻辑,例如:
    // axios.get(URL, { headers }).then(res => { /* 处理响应 */ });
  });
});

如果需要单独输出每组的结果(与你期望的格式一致),可以添加遍历打印逻辑:

processedGroups.forEach(group => {
  console.log(JSON.stringify(group, null, 2));
});

内容的提问来源于stack exchange,提问作者martyzee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 01:53:14