0

Hey guy's I have a table

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>

And I have an array to be filled with this data:

    var params = {
        "fruit":"apple",
        "test": "Nope",
        "values": [
        [Here,Must  ,Come the header ],
        [Here,must come ,the first table data],
        [Here must,comt the ,second tabel data]
        ],
    }

And this is just an example of 1 table some tables contain more rows.

How can I make sure the whole data comes in the array ?

Mr always wrong
  • 299
  • 3
  • 17

2 Answers2

2

You need nested loops to achieve the required result. See the below snippet

 var params = {
        "fruit":"apple",
        "test": "Nope",
        "values": [
       
        ],
    }
    
    $(function(){
        $('button').click(function() {
            $("#mytable tr").each(function(index, item){
              var temp = []
              if(index == 0) {
                $(item).find('th').each(function(idx, col){
                    temp.push($(col).text());
                })
              }
              $(item).find('td').each(function(idx, col){
                temp.push($(col).text());
              })
              params.values.push(temp)
            })
             console.log(params);
        })
      
    })
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td> 
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td> 
    <td>94</td>
  </tr>
</table>
<button>Click</button>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
1

You can use each() loop in combination with map() and get() to add to each table row as new array to params.values.

var params = {
  "fruit": "apple",
  "test": "Nope",
  "values": [],
}

$('table tr').each(function() {
  params.values.push($(this).find('*').map(function() {
    return $(this).text()
  }).get())
});

console.log(params)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

You can also instead of each() loop and push() just use map() and get() two times.

var params = {
  "fruit": "apple",
  "test": "Nope"
}

params.values = $('table tr').map(function() {
  return [$(this).find('*').map(function() {
    return $(this).text()
  }).get()]
}).get();

console.log(params)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176