Sorting table cells in right columns in JavaScript

Viewed 24

I think, this question will be obscure to understand, but try to describe it.enter image description here

If you take a glance on a table, cells that contain names (Nazarbek, Toshkent, Qo'qon ...) aren't in right columns (Columns also have names that includes cells' names). I wanted to sort them like in "Nazarbek" column should include only cells that named "Nazarbek", "Raximova" => "Raximova" etc. Cells that no common names with column should stay empty (Like the last 2 cells in the first row, 3 cells in the second row).
P.S: Trying to implement data from the MongoDb database in EJS file using Node.js.
I hope you understood.
There is code below for making sense or providing any hints, you don't have to correct my code. Any methods of achieving what I wanted is accepted.

<%- include('partials/header') -%>

<% console.log("-----//-----//-----//-----//-----//-----") %>
<table id="mainTable" width='100%' class="table table-bordered table-hover table-striped table-sm">
  <tr>
    <th></th>
    <% allAddresses.forEach((gtem) => { %>
      <th scope="col" style='writing-mode: vertical-rl; transform: rotate(180deg)'><%= gtem %></th>
    <% }) %>
  </tr>
  <% for (let item = 0; item < address.length; item++){ %>
    <% let stockIdentifier = address[item].rollingStockNumber.slice(11, 22) %>
    <% let arrayOfAddresses = _.values(address[item].addresses).sort() %>
    <tr>
      <th align="center"><%- stockIdentifier.slice(0, 4) + " " + stockIdentifier.slice(4, 6) + " " + stockIdentifier.slice(6, 10) + " " + stockIdentifier.slice(10, 11) %></th>
    <% var current = null; %>
    <% var count = 0; %>
    <% for (var index = 0; index < arrayOfAddresses.length; index++){ %>
      <% if (arrayOfAddresses[index] != current){ %>
        <% if (count > 0){ %>
          <td><%= count + " " + current %></td>
          <% allAddresses.forEach((address) => { %>
            <% if (current === address){ %>
              <% let cell = allAddresses.indexOf(address) %>
              <% console.log("The first part is " + (cell + 1)) %>
            <% } %>
          <% }) %>
        <% } %>
        <% current = arrayOfAddresses[index] %>
        <% count = 1 %>
      <% } else { %>
        <% count++ %>
      <% } %>
    <% } %>
    <% if (count > 0){ %>
      <td><%= count + " " + current %></td>
      <% allAddresses.forEach((address) => { %>
        <% if (current === address){ %>
          <% let cell = allAddresses.indexOf(address) %>
          <% console.log("The second part " + (cell + 1)) %>
        <% } %>
      <% }) %>
    <% } %>
    </tr>
  <% } %>
</table>
<form action="/indexCalculated.html" method="post">
    <button class="btn btn-dark" type="submit" name="backToMainPage" style="margin: 15px 10px 40px 742px; display: inline-block">Back</button>
</form>

<%- include('partials/footer') -%>

In app.js:

app.post('/indexReplaced.html', (req, res) => {

  Address.find({}, function(err, item){
    Address.aggregate([
      {
        $group: {
          _id: null,
          addresses: {
            $push: "$addresses"
          }
        }
      },
      {
        $project: {
          allAddresses: {
            $reduce: {
              input: "$addresses",
              initialValue: [],
              in: {
                "$concatArrays": [
                  "$$value",
                  "$$this"
                ]
              }
            }
          }
        }
      }
    ], function (err, addresses){
        function onlyUnique(value, index, self) {
          return self.indexOf(value) === index
        };
        let unique = addresses[0]["allAddresses"].filter(onlyUnique)
        // console.log(unique);
        res.render("indexCalculated", {
          address: item,
          allAddresses: unique.sort()
        });
    });
  })
});

Any support is much appreciated.

0 Answers
Related