在axios中为对象的键添加前缀

Adding a prefix to the key of an object in axios

我有对象

data = {
    others: [
        {
            code: "A", label: "0-A"
        },
        {
            code: "B", label: "0-B"
        },
        ...,
        {
            code: "N", label: "0-N"
        }
    ]
}

我需要将 other_ 前缀添加到 code 值(例如,other_N),然后再将其发送到 axios 查询:

await axios.post(`${URL}`, { data })

可以使用Array.map函数来操作数据对象

data.others.map(x => { return { other_code: x.code, label: x.label }})

像这样

let data = {
  others: [{
      code: "A",
      label: "0-A"
    },
    {
      code: "B",
      label: "0-B"
    },
    {
      code: "N",
      label: "0-N"
    }
  ]
};

let modifiedData = data.others.map(x => {
  x.code = `other_${x.code}`;
  return x;
})

console.log(modifiedData);