1

I need to make statistics of data which is fetched from an HTML table. The first column is gatewayId, the second one is statusId and the third one gsmError. I successfully filtered that table and pushed those values into an array.

Now I need to make a list of unique gateways from that array. I do not know which values are gonna be in that array, I just know that they are contained in column "10".

The array looks like this: [ "2951", "3", "511", "2737", "3", "502", "2737", "3", "502", "2732"...]

let messageLogTable = $("#tableId");
    var filteredColData = [];

    messageLogTable.find('tr').each(function(rowIndex, rowData) {
        if(rowIndex > 0){

            $(this).find('td').each(function(colIndex, colData) {

                if(colIndex === 10 || colIndex === 11 || colIndex === 17) {
                    filteredColData.push($.trim(colData.textContent));
                }
            });
        }
    });
    console.log(filteredColData);

The result should be like this: 2951, 2737, 2732

I would like that this is done using jQuery, if possible.

Thanks!

Nenson
  • 173
  • 2
  • 9
  • 1
    2737 appears twice – BrTkCa Sep 23 '16 at 14:02
  • It might be prudent to keep an array of `object`s with the `gatewayId`,`statusId`, and `gsmError` properties. From there you can filter it down. At least that would be easier than just keeping an array of seemingly non-descriptive values. – Orpheus Sep 23 '16 at 14:05
  • Hey, thanks for the feedback. I thought of the same, just was not sure how to store those values into an object. – Nenson Sep 23 '16 at 16:35
  • @Orpheus, if you could provide me the code for pushing those values into an object, that would be great. Like so: var gatewayList = { gatewayId: 123, statusId: 12, gsmError: 502 } – Nenson Sep 24 '16 at 09:20

2 Answers2

0

I would do this job as follows;

var  data = ["2951", "3", "511", "2737", "3", "502", "2737", "3", "502", "2732"],
gatewayID = [...new Set(data.reduce((p,c,i) => !(i%3) ? p.concat(c) : p, []))]
console.log(gatewayID)
Redu
  • 25,060
  • 6
  • 56
  • 76
0

You can create a list of discovered and use a filter.

var messageLogTable = $("#tableId");
var filteredColData = [];
var discovered = [];
messageLogTable.find('tr').each(function(rowIndex, rowData) {
    $(this).find('td').each(function(colIndex, colData) {
        if (filteredColData.length > 0) {
            for (var i = 0; i < filteredColData.length; i++) {
                if (filteredColData[i] == $(this).html()) {
                    discovered.push(filteredColData[i]);
                }
            };
        };
        filteredColData.push($(this).html());
    });
});
filteredColData = filteredColData.filter(function(x) {
    return discovered.indexOf(x) < 0
})
alert(JSON.stringify(filteredColData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="tableId">
<tr>
  <td>1</td>
  <td>2</td>
  <td>2</td>
  <td>3</td>
  <td>4</td>
  <td>4</td>
</tr>
</table>
BrTkCa
  • 4,703
  • 3
  • 24
  • 45