Categories
JavaScript Answers

How to compare decimal numbers with JavaScript?

Sometimes, we want to compare decimal numbers with JavaScript.

In this article, we’ll look at how to compare decimal numbers with JavaScript.

How to compare decimal numbers with JavaScript?

To compare decimal numbers with JavaScript, we can round both numbers and then compare them.

For instance, we write:

const a = 99.99
const b = 101.99
console.log((Math.round(a * 100) > Math.round(b * 100)))

to multiply a and b by 100 and call Math.round with the returned number to get rid of the decimals.

Then we use > to compare them.

Since a is smaller than b, the console log should log false.

Conclusion

To compare decimal numbers with JavaScript, we can round both numbers and then compare them.

Categories
JavaScript Answers

How to delete an item from list with React and JavaScript?

Sometimes, we want to delete an item from list with React and JavaScript.

In this article, we’ll look at how to delete an item from list with React and JavaScript.

How to delete an item from list with React and JavaScript?

To delete an item from list with React and JavaScript, we can use some array methods.

For instance, we write:

import React from "react";

const arr = [
  { id: 1, name: "apple" },
  { id: 2, name: "orange" },
  { id: 3, name: "grape" }
];

export default function App() {
  const [items, setItems] = React.useState(arr);

  const deleteItem = (index) => () =>
    setItems((items) => items.filter((_, i) => i !== index));

  return (
    <>
      {items.map((it, index) => {
        return (
          <div key={it.id}>
            {it.name} <button onClick={deleteItem(index)}>delete</button>
          </div>
        );
      })}
    </>
  );
}

to create the items state with useState.

Then we define the deleteItem function that takes the index of the item to delete and returns a function that calls setItems with (items) => items.filter((_, i) => i !== index) to set items to an array that doesn’t include the item in items at index.

Next, we call items.map to render the items with a delete button in each entry.

We set the onClick prop of the button to the function returned by deleteItem when called with the index.

Conclusion

To delete an item from list with React and JavaScript, we can use some array methods.

Categories
JavaScript Answers

How to open a file dialog box when an a tag is clicked with JavaScript?

Sometimes, we want to open a file dialog box when an a tag is clicked with JavaScript.

In this article, we’ll look at how to open a file dialog box when an a tag is clicked with JavaScript.

How to open a file dialog box when an a tag is clicked with JavaScript?

To open a file dialog box when an a tag is clicked with JavaScript, we can call e.preventDefault on the a element’s click handler.

And then we call input.click to open the file dialog.

For instance, we write:

<input type="file" multiple style='display: none' />
<a>Upload</a>

to add a file input and an a element.

Then we write:

const a = document.querySelector('a')
const input = document.querySelector('input')
a.onclick = (e) => {
  e.preventDefault()
  input.click()
}

to select the a and input elements.

And then we set a.onclick to a function that calls e.preventDefault to stop the default behavior for links.

And then we call input.click to open the file dialog.

Conclusion

To open a file dialog box when an a tag is clicked with JavaScript, we can call e.preventDefault on the a element’s click handler.

And then we call input.click to open the file dialog.

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.