How to convert a form to a Json array in javascript

mars

I am trying to convert a form result to this format below.

{
  "project": {
    "id": 5,
    "name": "max",
    "identifier": "max_project",
    "description": "This is description",
  }
}

But result is something like this,

{name: "max", identifier: "max_project", description: "This is description"}

Please help me to correct the code to get the intended result. I am trying to post the result in jsonp format.

$("#submit").on('click', function() {
  var data = {};

  $("#form").serializeArray().map(function(x) {
    data[x.name] = x.value;
  });
  console.log(data);
})
<form id="form" action="submit" method="post">
  Name:
  <input type="text" name="name">
  <br>identifier:
  <input type="text" name="identifier">
  <br>description:
  <textarea type="text" name="description"></textarea>
  <br>
  <input id="submit" type="button" name="submit" value="submit">
</form>

Omar Mowafi

The simplest solution to achieve your result is the following

$("#submit").on('click', function() {
  var data = {project:{}};

  $("#form").serializeArray().map(function(x) {
    data["project"][x.name] = x.value;
  });
  console.log(data);
})

Or the way you did it but and put it in another hash

$("#submit").on('click', function() {
  var project = {};

  $("#form").serializeArray().map(function(x) {
    project[x.name] = x.value;
  });
  var data = {project: project}
  console.log(data);
})

There are many ways to achieve the same result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert JSON object to JavaScript array?

How to convert JSON to Array in Javascript

How to convert json to array object in javascript?

Convert JSON string to an array in multistep Form in JavaScript?

Convert html form array to formated json array

How to Convert nested json to array of object in Javascript?

How to convert mySQL JSON column into javascript array?

how to convert json to array in javascript for an li list

how to convert array api response to json in JavaScript

How to convert `pid` in json array into array of `children` form?

javascript - convert string to json array

How to convert json encoded PHP array to an array in Javascript?

how to convert json array to javascript array

How to convert dynamic JSON string to javascript array

How to convert Array into JSON

How to convert JSON Object into Javascript array

How to convert php array to javascript's varname.arrkey form?

how to convert JSON array into JavaScript 2D array

how to convert this JavaScript object that contains an array to JSON?

Convert javascript array into json array

How to convert an array of JSON into an array of javascript object?

using Javascript how can I access elements form a json array?

How to send Javascript array as Json combined with a html form?

How to convert Json to array?

How to form This JSON from this Javascript array of arrays?

How to convert JSON array to OBJECT array javascript?

how to access json object that is in a form of array in javascript

How to read a file and convert to json array in javascript?

How to convert Array of javascript objects to form-encoded for POST?