parse nested javascript arrayHow do JavaScript closures work?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Replace a motion-sensor/timer with simple single pole switch
Ways you can end up paying interest on a credit card if you pay the full amount back in due time
How to run a command 1 out of N times in Bash
Can users with the same $HOME have separate bash histories?
What is the motivation behind designing a control stick that does not move?
Can my UK debt be collected because I have to return to US?
How to find better food in airports
Function of the separated, individual solar cells on Telstar 1 and 2? Why were they "special"?
Killing task by name - start menu shortcut
Is it good practice to speed up and slow down where not written in a song?
Cheap oscilloscope showing 16 MHz square wave
Why do we need explainable AI?
Why is Mitch McConnell blocking nominees to the Federal Election Commission?
Different past tense for various *et words
In Toy Story, are toys the only inanimate objects that become alive? And if so, why?
D Scale Question
Colored grid with coordinates on all sides?
Can Russians naturally pronounce "попал в бесперспективняк"?
Are there consequences for not filing a DMCA (any country)
Why are CEOs generally fired rather being demoted?
Does the telecom provider need physical access to the SIM card to clone it?
Single vs Multiple Try Catch
Displaying Time in HH:MM Format
How to solve this inequality , when there is a irrational power?
parse nested javascript array
How do JavaScript closures work?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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
javascript reactjs ecmascript-6
add a comment |
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
javascript reactjs ecmascript-6
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 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
– Trentfrompunchbowl1
Mar 28 at 1:30
add a comment |
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
javascript reactjs ecmascript-6
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
javascript reactjs ecmascript-6
javascript reactjs ecmascript-6
asked Mar 28 at 0:52
Trentfrompunchbowl1Trentfrompunchbowl1
355 bronze badges
355 bronze badges
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 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
– Trentfrompunchbowl1
Mar 28 at 1:30
add a comment |
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 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
– Trentfrompunchbowl1
Mar 28 at 1:30
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 at 1:12
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 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
– Trentfrompunchbowl1
Mar 28 at 1:30
@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
– Trentfrompunchbowl1
Mar 28 at 1:30
add a comment |
3 Answers
3
active
oldest
votes
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;
);
);
add a comment |
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);
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388616%2fparse-nested-javascript-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
);
);
add a comment |
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;
);
);
add a comment |
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;
);
);
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;
);
);
answered Mar 28 at 1:01
Jaromanda XJaromanda X
38.8k4 gold badges36 silver badges57 bronze badges
38.8k4 gold badges36 silver badges57 bronze badges
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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);
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);
answered Mar 28 at 1:11
brian-lives-outdoorsbrian-lives-outdoors
15.6k1 gold badge18 silver badges46 bronze badges
15.6k1 gold badge18 silver badges46 bronze badges
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 28 at 1:15
Ras AbdoellahRas Abdoellah
363 bronze badges
363 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388616%2fparse-nested-javascript-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
there is no nested array in the question ...
– Jaromanda X
Mar 28 at 0:57
the nested object sorry the concrete_cylinders
– Trentfrompunchbowl1
Mar 28 at 1:01
All good, I kinda figured that is what you wanted to deal with - see answer
– Jaromanda X
Mar 28 at 1:02
If they are numeric in object as shown then they should stay numeric when parsed to json
– charlietfl
Mar 28 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
– Trentfrompunchbowl1
Mar 28 at 1:30