Categories
JavaScript Answers

How to convert a table to JSON with JavaScript?

Sometimes, we want to convert a table to JSON with JavaScript.

In this article, we’ll look at how to convert a table to JSON with JavaScript.

How to convert a table to JSON with JavaScript?

To convert a table to JSON with JavaScript, we can get the rows with with table.rows and the table header with table.tHead.

For instance, we write:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
    </tr>
    <tr>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.querySelector('table')
const [header] = table.tHead.rows
const props = [...header.cells].map(h => h.textContent)
const rows = [...table.rows].map(r => {
  const entries = [...r.cells].map((c, i) => {
    return [props[i], c.textContent]
  })
  return Object.fromEntries(entries)
})
console.log(rows)

to select the table with querySelector.

Next, we get the header row with

const [header] = table.tHead.rows

Next, we spread the header.cells into an array and get the textContent of each cell to get the property keys into an array and assign it to props.

Then we get the rows with table.rows and spread the entries into an array.

And then we call map with a callback to return the key and the textContent of the cell and put it into an array and return it.

Then we call Object.fromEntries with entries to return the object with the table row property keys and values.

Therefore, rows is

[{
  Column 1: "Column 1",
  Column 2: "Column 2",
  Column 3: "Column 3"
}, {
  Column 1: "A1",
  Column 2: "A2",
  Column 3: "A3"
}, {
  Column 1: "B1",
  Column 2: "B2",
  Column 3: "B3"
}, {
  Column 1: "C1",
  Column 2: "C2",
  Column 3: "C3"
}]

Conclusion

To convert a table to JSON with JavaScript, we can get the rows with with table.rows and the table header with table.tHead.

Categories
JavaScript Answers

How to remove an iframe with JavaScript?

Sometimes, we want to remove an iframe with JavaScript.

In this article, we’ll look at how to remove an iframe with JavaScript.

How to remove an iframe with JavaScript?

To remove an iframe with JavaScript, we can use the remove method.

For instance, we write:

<iframe>

</iframe>

to add an iframe.

Then we write:

const iframe = document.querySelector('iframe')
iframe.remove()

to select the iframe with querySelector.

And then we call remove to remove it.

Conclusion

To remove an iframe with JavaScript, we can use the remove method.

Categories
JavaScript Answers

How to get the last item in a JavaScript object with a for loop?

Sometimes, we want to get the last item in a JavaScript object with a for loop.

In this article, we’ll look at how to get the last item in a JavaScript object with a for loop.

How to get the last item in a JavaScript object with a for loop?

To get the last item in a JavaScript object with a for loop, we can use the Object.entries method.

For instance, we write:

const obj = {
  foo: 1,
  bar: 2,
  baz: 3
}
const last = Object.entries(obj).at(-1)
console.log(last)

to call Object.entries with obj to return an array of key-value pair arrays from the object.

Then we call at with -1 to return the last item in the array.

Therefore, last is ['baz', 3].

Conclusion

To get the last item in a JavaScript object with a for loop, we can use the Object.entries method.

Categories
JavaScript Answers

How to make a live clock with JavaScript?

Sometimes, we want to make a live clock with JavaScript.

In this article, we’ll look at how to make a live clock with JavaScript.

How to make a live clock with JavaScript?

To make a live clock with JavaScript, we can use the setInterval function.

For instance, we write:

<span></span>

to add a span.

Then we write:

const span = document.querySelector('span');

const time = () => {
  const d = new Date();
  const s = d.getSeconds().toString().padStart(2, '0');
  const m = d.getMinutes().toString().padStart(2, '0');
  const h = d.getHours().toString().padStart(2, '0');
  span.textContent = `${h}:${m}:${s}`
}

setInterval(time, 1000);

to select the span with querySelector.

Then we define the time function that populates the span with the time.

In it, we create a Date instance that returns the current date time.

Then we get get hours, minutes, and seconds with getHours, getMinutes, and getSeconds.

We convert them to strings with toString.

And we prepend a 0 to each string if their length is less than 2 with padStart.

Next, we set span.textContent to the time string to show the current time.

Finally, we call setInterval with time and 1000 to call time every second.

Conclusion

To make a live clock with JavaScript, we can use the setInterval function.

Categories
JavaScript Answers

How to export HTML table to excel using JavaScript?

Sometimes, we want to export HTML table to excel using JavaScript.

In this article, we’ll look at how to export HTML table to excel using JavaScript.

How to export HTML table to excel using JavaScript?

To export HTML table to excel using JavaScript, we can select the table, get its HTML, and encode it into a base64 string.

For instance, we write:

<table id="table">
  <thead>
    <tr>
      <th>name</th>
      <th>place</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>adfas</td>
      <td>asdfasf</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.getElementById('table');
const html = table.outerHTML;
window.open('data:application/vnd.ms-excel;base64,' + btoa(html));

to select the table with getElementById.

And then we get its HTML code with outerHTML.

Next, we call btoa to convert the table HTML string into a base64 string.

And we concatenate the 'data:application/vnd.ms-excel;base64,' MIME type before it.

Then we download it with window.open.

Conclusion

To export HTML table to excel using JavaScript, we can select the table, get its HTML, and encode it into a base64 string.