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

JavaScript中给对象数组除team外的所有键名添加Agst后缀的实现方法

JavaScript批量修改对象数组键名方案

核心实现逻辑:遍历数组内的每个对象,动态枚举所有键名,仅排除team字段,其余字段自动添加Agst后缀,无需手动指定待修改的键列表,适配单对象超过100个键的场景。

推荐方案(非破坏性,返回新数组不修改原数据)

// 原数组
const originalArr = [
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 },
  { team: 1, stat1: 17, stat2: 25, var1: 'okay', var3: 'nah', here: 24 }
];

// 处理逻辑
const processedArr = originalArr.map(item => {
  const result = {};
  for (const key in item) {
    // 排除team字段直接保留,其余字段加后缀
    result[key === 'team' ? key : `${key}Agst`] = item[key];
  }
  return result;
});

可选方案(破坏性,直接修改原数组)

如果不需要保留原数组数据,可以用这个更节省内存的方案:

originalArr.forEach(item => {
  Object.keys(item).forEach(key => {
    if (key !== 'team') {
      item[`${key}Agst`] = item[key];
      // 删除原键
      delete item[key];
    }
  });
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:15:03