猿问

解析嵌套的javascript数组

我有这个数组


  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)

      }

    });

谢谢您的协助


哆啦的时光机
浏览 182回答 3
3回答

慕神8447489

这是使用较新的传播语法的版本: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);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答