2

I have this array

  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
    }
  ]

I'm trying to parse them when i post the data ( formik modifies them back to text so i am required to parse them as int for my backend )

My post looks like this ( this works except for the nested objects i want them parsed as integer also )

axios.post('http://localhost:8123/samples/concrete', {
  air_content: parseFloat(air_content),
  compaction_method: parseInt(compaction_method),
  concrete_cylinders
});

the psuedo/My best try of the code of what I'm trying to do is the below

   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)
      }
    });

Thankyou for assistance

5
  • there is no nested array in the question ... Commented Mar 28, 2019 at 0:57
  • the nested object sorry the concrete_cylinders Commented Mar 28, 2019 at 1:01
  • All good, I kinda figured that is what you wanted to deal with - see answer Commented Mar 28, 2019 at 1:02
  • If they are numeric in object as shown then they should stay numeric when parsed to json Commented Mar 28, 2019 at 1:12
  • @charlietfl There is something weird with formik when tho i have them set to number convert back to string somehow.. i've searched alot on google and cannot find a solution Commented Mar 28, 2019 at 1:30

3 Answers 3

3

before calling axios.post you'll need to

concrete_cylinders.forEach(x => {
    x.id = parseInt(x.id);
    x.curing = parseInt(c.curing);
});

or, if you really want, you can do it like

axios.post('http://localhost:8123/samples/concrete', {
  air_content: parseFloat(air_content),
  compaction_method: parseInt(compaction_method),
  concrete_cylinders: concrete_cylinders.map(x => {
    x.id = parseInt(x.id);
    x.curing = parseInt(c.curing);
    return x;
  });
});
Sign up to request clarification or add additional context in comments.

Comments

2

Here's a version using the newer spread syntax:

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);

Comments

1

You could always try using forEach on the array before posting. So for example...

pojo = {
  air_content: '',
  compaction_method: 1,
  concrete_cylinders: [
    {
      id: '3',
      specimen_name: 'A',
      mould_number: '',
      curing: '1',
      age: 7
    },
    {
      id: '3',
      specimen_name: 'A',
      mould_number: '',
      curing: '1',
      age: 7
    },
    {
      id: '3',
      specimen_name: 'A',
      mould_number: '',
      curing: '1',
      age: 7
    }
  ]
}

pojo.concrete_cylinders.forEach(e => {
  e.id = parseFloat(e.id)
  e.curing = parseInt(e.curing)
//...anything else you want to change before posting
})

Then pass the object to your axios.post

axios.post('http://localhost:8123/samples/concrete', pojo);

I'm sure there's a way to do this in less lines, but this should solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.