我有这个数组 air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 } ]我试图在发布数据时解析它们(formik将它们修改回文本,因此我需要将它们解析为int的后端)我的帖子看起来像这样(除了嵌套对象之外,我也希望它们也解析为整数)axios.post('http://localhost:8123/samples/concrete', { air_content: parseFloat(air_content), compaction_method: parseInt(compaction_method), concrete_cylinders});psuedo /我正在尝试做的代码的最佳尝试如下 axios.post('http://localhost:8123/samples/concrete', { air_content: parseFloat(air_content), compaction_method: parseInt(compaction_method), concrete_cylinders: { [concrete_cylinders.id]: parseInt(concrete_cylinders.id), [concrete_cylinders.curing]: parseInt(concrete_cylinders.curing) } });谢谢您的协助
3 回答

慕神8447489
TA贡献1780条经验 获得超1个赞
这是使用较新的传播语法的版本:
const concrete_cylinders = [
{
id: '',
specimen_name: 'A',
mould_number: '',
curing: '1',
age: '7'
},
{
id: '',
specimen_name: 'A',
mould_number: '',
curing: '1',
age: '7'
},
{
id: '',
specimen_name: 'A',
mould_number: '',
curing: '1',
age: '7'
}
]
const result = concrete_cylinders.map(o => ({
...o,
...{
curing: parseInt(o.curing),
age: parseInt(o.age)
}
}));
console.log(result);
添加回答
举报
0/150
提交
取消