2

I have a really big HTML Table with data formatted like this (a simplified version of my table):

https://pastebin.com/K5UB4cGB

The list is much bigger.

I need to know which Application runs on which Server(multiple).

I now need to somehow get all the data and store it (maybe in an array) so I can work with it. (For example I need to compare certain applications and write a code to document it.)

I've already tried many methods I found on the web but I not sure im working in the right way.

My recent atempt is:

var data = Array();

$("table").each(function(i, v) {
  data[i] = Array();
  $(this).each(function(ii, vv) {
    data[i][ii] = $(this).text();
  });
})

document.write(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
  </tr>
</table>


<div id="result" style="color: #f00"></div>

Outputs:

1 2 3 4 6 7 8 9 10 

With this method I get one array:

1,2,3,4,5,6,7,8,9,10

But I cannot access the stored vaules because for example data[0] outputs all the values and not only one.

Sorry for the long text

Thanks for your help :)

Kind Regards, Markus

4 Answers 4

1

You can try using nested for loops and get a nested array of data.

let data = [];

$("table tr").each(function() {
  let row = [];
  $(this).children("td").each(function() {
    row.push($(this).text());
  })
  data.push(row);
})

console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="table">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>6</td>
    <td>7</td>
  </tr>
  <tr>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to iterate rows and within rows iterate cells

You can do this all with map()

var data = $('tr').map(function() {
  return [$(this).children().map(function() {
    return $(this).text().trim()
  }).get()];
}).get();



console.log(JSON.stringify(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>6</td>
    <td>7</td>
  </tr>
  <tr>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>


<div id="result" style="color: #f00"></div>

3 Comments

Is there a way I can sort the values by its row? So for example 3 arrays for 123, 456, 789 ? I need to get all columnwise. So for example (1,4,7) (2,5,8) (3,6,9). Is there a way I can do that?
What i the output of the script? I dont see any output only the table?
Hit F12 and look in developer tools console. That's what console.log() does...outputs there
0

use each to loop through td and get its text value

var val=[]
$('#table td').each(function(e){
val.push($(this).text())
})
console.log(val)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<table id="table">
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>4</td>
      <td>6</td>
      <td>7</td>
    </tr>
    <tr>
        <td>8</td>
        <td>9</td>
        <td>10</td>
      </tr>
</table>

2 Comments

@House97_ can you please verify it if it is the result that you expected,thanks
Yes it worked but my problem is: The file I need to get the data from has the a table header (th) but I also need to add that to the array.... Sorry I wrote a comment but it was deleted somehow. Is there a way I can sort the values by its column? I need to get all columnwise. So for example (1,4,7) (2,5,8) (3,6,9). Is there a way I can do that?
0

In the following I am using an each function to derive the data of the table and then pushing into an array list. I hope this code is helpful. This code is really small.

var arr = [];
$("th, td").each(function(){
  arr.push($(this).text());
  $("#result").text(arr);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>6</td>
    <td>7</td>
  </tr>
  <tr>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
</table>


<div id="result" style="color: #f00"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.