Trentfrompunchbowl1
Trentfrompunchbowl1

Reputation: 109

parse nested javascript array

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

Upvotes: 2

Views: 98

Answers (3)

Ras Abdoellah
Ras Abdoellah

Reputation: 36

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.

Upvotes: 1

Brian Adams
Brian Adams

Reputation: 45810

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

Upvotes: 2

Jaromanda X
Jaromanda X

Reputation: 1

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

Upvotes: 3

Related Questions