JavaScript How to convert form inside table into JSON

I am trying to convert form data into json I have a html table as following. On form submit what i want to do is convert it to json

<form class="sales-order-form">
    <table class="table">
        <tbody>
        <tr>
            <th>Item Name</th>
            <th>Unit Price</th>
            <th>Quantity</th>
            <th>Free Quantity</th>
            <th>Sub Total</th>
        </tr>
        <tr>
            <td><select class="form-control" name="itemName" style="width: 250px;">
                <option></option>
                <option>Option1</option>
                <option>Option2</option>
                <option>Option3</option>
            </select></td>
            <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
            </td>
        </tr>
        <tr>
            <td><select class="form-control" name="itemName" style="width: 250px;">
                <option></option>
                <option>Option1</option>
                <option>Option2</option>
                <option>Option3</option>
            </select></td>
            <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
            </td>
        </tr>
        </tbody>
    </table>
</form>

On form submit how do I convert this form into following json?

[
  {
    "itemName": "item1",
    "unitPrice": "49"
  },
  {
    "itemName": "item2",
    "unitPrice": "56"
  }
]

How to do this??

I tried following

function getFormData($salesOrderForm){
        var unindexed_array = $salesOrderForm.serializeArray();
        var indexed_array = {};

        $.map(unindexed_array, function(n, i){
            indexed_array[n['name']] = n['value'];
        });

        return indexed_array;
    }

But it only returns one json

{
  "itemName": "item1",
  "unitPrice": "50"
}
Answer:1

You can get elements details by using document.getElementsByName("unitPrice") and document.getElementsByName("itemName")

Try this:

  function getFormData() {
    let res = [];
    let x = document.getElementsByName("unitPrice");
    let y = document.getElementsByName("itemName");

    for (let i = 0; i < x.length; i++) {
      let obj = {
        "itemName": x[i].value,
        "unitPrice": y[i].value
      }
      res.push(obj);
    }
    console.log(res);
  }
  <form class="sales-order-form">
    <table class="table">
      <tbody>
        <tr>
          <th>Item Name</th>
          <th>Unit Price</th>
          <th>Quantity</th>
          <th>Free Quantity</th>
          <th>Sub Total</th>
        </tr>
        <tr>
          <td><select class="form-control" name="itemName" style="width: 250px;">
              <option></option>
              <option>Option1</option>
              <option>Option2</option>
              <option>Option3</option>
            </select></td>
          <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
          </td>
        </tr>
        <tr>
          <td><select class="form-control" name="itemName" style="width: 250px;">
              <option></option>
              <option>Option1</option>
              <option>Option2</option>
              <option>Option3</option>
            </select></td>
          <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
          </td>
        </tr>
      </tbody>
    </table>
  </form>
  <button onclick="getFormData()">getFormData</button>
Answer:2

You can try with jQuery's .map() and .get()

$('form').submit(function(){
  var json = $('.table tr:gt(0)').map(function(){
    return {
      itemName: $(this).find('select option:selected').text(), 
      unitPrice: $(this).find('[name=unitPrice]').val()
    }
  }).get();
  console.log(json);
  return false; // added to stay on the page for testing purpose
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="sales-order-form">
    <table class="table">
        <tbody>
        <tr>
            <th>Item Name</th>
            <th>Unit Price</th>
            <th>Quantity</th>
            <th>Free Quantity</th>
            <th>Sub Total</th>
        </tr>
        <tr>
            <td><select class="form-control" name="itemName" style="width: 250px;">
                <option></option>
                <option>Option1</option>
                <option>Option2</option>
                <option>Option3</option>
            </select></td>
            <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
            </td>
        </tr>
        <tr>
            <td><select class="form-control" name="itemName" style="width: 250px;">
                <option></option>
                <option>Option1</option>
                <option>Option2</option>
                <option>Option3</option>
            </select></td>
            <td><input class="form-control" name="unitPrice" type="text" readonly="readonly" value="50">
            </td>
        </tr>
        </tbody>
    </table>
    <input type="submit" id="Submit" value="Submit"/>
</form>
Answer:3

Here is what I have already: myFunct({ myObj: { db } }) I need to add another function in such as: myFunct({ myObj: async ({ req }) => { //more scripts } }) What I tried and failed: myFunct({...

Here is what I have already: myFunct({ myObj: { db } }) I need to add another function in such as: myFunct({ myObj: async ({ req }) => { //more scripts } }) What I tried and failed: myFunct({...

  1. declaring async function javascript
  2. declaring async function
  3. declare async function typescript
  4. declare async function js
  5. declare async function nodejs
  6. declare async function es6
  7. declare async function react
  8. declare async function c#
  9. declare async arrow function
  10. node declare async function
  11. typescript declare async function type
  12. ts declare async function

I have a HTML select, and using the Chosen JS plugin, for multiple select. Chosen uses the CSS property background-image and others to set the style for each select option (always the same style). ...

I have a HTML select, and using the Chosen JS plugin, for multiple select. Chosen uses the CSS property background-image and others to set the style for each select option (always the same style). ...

  1. possible different colors

I've got the following array and I'd like to return a new array containing the count of the duplicate ids along with the value of the id: const things = [ { id: 1, title: 'Something', ...

I've got the following array and I'd like to return a new array containing the count of the duplicate ids along with the value of the id: const things = [ { id: 1, title: 'Something', ...

  1. count duplicate values in excel
  2. count duplicate values in sql
  3. count duplicate values in a column in excel
  4. count duplicate values only once in excel
  5. count duplicate values in list python
  6. count duplicate values in r
  7. count duplicate values pandas
  8. count duplicate values in google sheets
  9. count duplicate values in array javascript
  10. count duplicate values in hashmap
  11. count duplicate values in excel pivot table
  12. count duplicate values in array php
  13. count duplicate values in dictionary python
  14. count duplicate values in column sql
  15. count duplicate values in array c#
  16. count duplicate values in mysql
  17. count duplicate values in power bi
  18. count duplicate values in array java
  19. count duplicate values in two columns excel
  20. count duplicate values in a column

my goal for this code is to make it so when one of the tree branches is clicked, it changes the clicked ^ from a ^ to a O using JQuery and Javascript (this is for a school assignment). For example, if ...

my goal for this code is to make it so when one of the tree branches is clicked, it changes the clicked ^ from a ^ to a O using JQuery and Javascript (this is for a school assignment). For example, if ...