David Sonnenfeld
David Sonnenfeld

Reputation: 67

html table to json - modify cells

Well, I have following html table:

  <table id="people" border="1">
    <thead>
      <tr>
        <th>Name</th>
        <th>Places</th>
        <th>Grade</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Oscar</td>
        <td>New York</td>
        <td>16.5</td>
      </tr>
      <tr>
        <td>Antonio</td>
        <td>
          <p>New York</p>
          <p>Chicago</p>
          <p>Toronto</p>
        </td>
        <td>14</td>
      </tr>
      <tr>
        <td>Jessica</td>
        <td>New York</td>
        <td>19</td>
      </tr>
    </tbody>   

Now when I use the jquery tableToJson function I get this output:

[
    {"Name":"Oscar","Places":"New York","Grade":"16.5"},
    {"Name":"Antonio","Places":"New York\n\t\t\tChicago\n\t\t\t\tToronto","Grade":"14"},
    {"Name":"Jessica","Places":"New York","Grade":"19"}
]

Now look at Antonio, the function created

{"Name":"Antonio","Places":"New York\n\t\tChicago\n\t\t\t\tToronto","Grade":"14"}

But instead of

\n

I want places also to be displayed as a JSON array like this:

{"Name":"Antonio","Places":["New York","Chicago","Toronto"],"Grade":"14"}

I know there are options described here (https://github.com/lightswitch05/table-to-json), but I don't know which one to use, or how to use them, as it is not really documented, how to use them in detail.

Any help would be really appreciated :-)

Upvotes: 0

Views: 56

Answers (1)

Akshay
Akshay

Reputation: 14348

If you want to update the existing array try using this

var places = [
    {"Name":"Oscar","Places":"New York","Grade":"16.5"},
    {"Name":"Antonio","Places":"New York\n Chicago\n Toronto","Grade":"14"},
    {"Name":"Jessica","Places":"New York","Grade":"19"}
];

for(var x=0;x<places.length;x++){
  places[x].Places = places[x].Places.split("\n");
}
console.log(places);
  

Upvotes: 1

Related Questions