简体   繁体   中英

how to convert html table into json desired format

I have a html table that i want to convert into json format but i'm not getting correctly.

the resultant json is not coming according to my format

here is my table

<table class="table" id="example-table">
   <thead>
      <tr>
         <th>Product Name</th>
         <th>Price</th>
         <th>Quantity</th>
         <th>Amount</th>
      </tr>
   </thead>
   <tbody>
      <tr class="allTheQuotationRow">
         <td>flex board</td>
         <td contenteditable="" class="priceChangeField">3</td>
         <td contenteditable="" class="quantityChangeField">5</td>
         <td>15</td>
      </tr>
      <tr class="allTheQuotationRow">
         <td>sign board</td>
         <td contenteditable="" class="priceChangeField">20</td>
         <td contenteditable="" class="quantityChangeField">1</td>
         <td>20</td>
      </tr>
      <tr class="allTheQuotationRow">
         <td>flex board</td>
         <td contenteditable="" class="priceChangeField">30</td>
         <td contenteditable="" class="quantityChangeField">1</td>
         <td>30</td>
      </tr>
      <tr class="allTheQuotationRow">
         <td>sign board</td>
         <td contenteditable="" class="priceChangeField">200</td>
         <td contenteditable="" class="quantityChangeField">19</td>
         <td>3800</td>
      </tr>
      <tr id="lastTotalRow">
         <td>total</td>
         <td></td>
         <td></td>
         <td>3865</td>
      </tr>
   </tbody>
</table>

i want my desired result to be like this:

{
   "flex_board": [
      {
         "Price": 3,
         "Quantity": 5,
         "Amount": 15
      },
      {
         "Price": 30,
         "Quantity": 1,
         "Amount": 30
      }
   ],
   "sign_board": [
      {
         "Price": 20,
         "Quantity": 1,
         "Amount": 20
      },
      {
         "Price": 200,
         "Quantity": 19,
         "Amount": 3800
      }
   ],
   "total": [
      {
         "Price": null,
         "Quantity": null,
         "Amount": 3865
      }
   ]
}

here is my jsfiddle: http://jsfiddle.net/eabangalore/cCzqn/1601/

Please help me thanks in advance!!!

Use querySelectorAll and Array.from to iterate the rows ( Comments inline )

var allRows = Array.from( document.querySelectorAll( "tbody tr:not(:last-child)" ) ); //get all rows except last one

var map = {};

allRows.forEach( function( row ){
  var cells = row.children;
  var prodName = cells[0].innerText; //index by product name
  map[ prodName ] = map[ prodName ] || []; //initialize inner array
  map[ prodName ].push({ //push each row to the respective product name's index
     Price : cells[1].innerText,
     Quantity : cells[2].innerText,
     Amount : cells[3].innerText
  });
});

console.log( map );

Demo

 var allRows = Array.from( document.querySelectorAll( "tbody tr:not(:last-child)" ) ); //get all rows except last one var map = {}; allRows.forEach( function( row ){ var cells = row.children; var prodName = cells[0].innerText; //index by product name map[ prodName ] = map[ prodName ] || []; //initialize inner array map[ prodName ].push({ //push each row to the respective product name's index Price : cells[1].innerText, Quantity : cells[2].innerText, Amount : cells[3].innerText }); }); console.log( map ); 
 <table class="table" id="example-table"> <thead> <tr> <th>Product Name</th> <th>Price</th> <th>Quantity</th> <th>Amount</th> </tr> </thead> <tbody> <tr class="allTheQuotationRow"> <td>flex board</td> <td contenteditable="" class="priceChangeField">3</td> <td contenteditable="" class="quantityChangeField">5</td> <td>15</td> </tr> <tr class="allTheQuotationRow"> <td>sign board</td> <td contenteditable="" class="priceChangeField">20</td> <td contenteditable="" class="quantityChangeField">1</td> <td>20</td> </tr> <tr class="allTheQuotationRow"> <td>flex board</td> <td contenteditable="" class="priceChangeField">30</td> <td contenteditable="" class="quantityChangeField">1</td> <td>30</td> </tr> <tr class="allTheQuotationRow"> <td>sign board</td> <td contenteditable="" class="priceChangeField">200</td> <td contenteditable="" class="quantityChangeField">19</td> <td>3800</td> </tr> <tr id="lastTotalRow"> <td>total</td> <td></td> <td></td> <td>3865</td> </tr> </tbody> </table> 

Refactoring an array like that can be a complicated procedure, but thankfully there is a library called lodash that has a function called groupBy. It can fix your problem in a single line of code! _.groupBy(table, "Product Name")

That's really it! Lodash Library

http://jsfiddle.net/nhc6m1af/

You can use reduce to convert your Array into desired Object

$('#convert-table').click(function() {
  var table = $('#example-table').tableToJSON();
  var result = table.reduce(function(acc, item) {
    var key = item["Product Name"].replace(/ /g, "_");
    if (!acc[key]) {
      acc[key] = []
    }
    acc[key].push({
      Price: item.Price,
      Quantity: item.Quantity,
      Amount: item.Amount
    })
    return acc;
  }, {});
  console.log(result);
});

jsFiddle Link : http://jsfiddle.net/scorpy2007/cCzqn/1603/

You can use same tableToJSON data object in generating your customized format as shown below

 var jsondata={};
  table.forEach( function( data ){  
  var prodName = data["Product Name"];
  jsondata[ prodName ] = jsondata[ prodName ] ?jsondata[ prodName ]: [];  
  jsondata[ prodName ].push({   
     Price : data['Price'],
     Quantity : data['Quantity'],
     Amount : data['Amount']
  });
});
console.log(JSON.stringify(jsondata)); 

Working fiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM