为了账号安全,请及时绑定邮箱和手机立即绑定

解析嵌套的javascript数组

解析嵌套的javascript数组

哆啦的时光机 2021-04-06 13:10:10
我有这个数组  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);


查看完整回答
反对 回复 2021-04-08
  • 3 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号