Categories
JavaScript Answers TypeScript

How to access class member from a function in method class in TypeScript?

Sometimes, we want to access class member from a function in method class in TypeScript.

In this article, we’ll look at how to access class member from a function in method class in TypeScript.

How to access class member from a function in method class in TypeScript?

To access class member from a function in method class in TypeScript, we can use the this keyword.

For instance, we write:

class Foo {
  bar() {}
  baz() {
    this.bar()
  }
}

to call the bar method in the Foo by using this.bar as we did in the baz method.

Conclusion

To access class member from a function in method class in TypeScript, we can use the this keyword.

Categories
JavaScript Answers

How to open Blob URL on Chrome iOS with JavaScript?

Sometimes, we want to open Blob URL on Chrome iOS with JavaScript.

In this article, we’ll look at how to open Blob URL on Chrome iOS with JavaScript.

How to open Blob URL on Chrome iOS with JavaScript?

To open Blob URL on Chrome iOS with JavaScript, we can use the FileReader constructor.

For instance, we write:

const reader = new FileReader();
const out = new Blob(['abc'], {
  type: 'text/plain'
});
reader.onload = (e) => {
  console.log(reader.result)
}
reader.readAsDataURL(out);

We create a new FileReader instance.

Then we create a new Blob instance that we pass into the readAsDataURL method to read the blob into a base64 data URL.

Then in the reader.onload method, we get the data URL from the reader.result property.

Conclusion

To open Blob URL on Chrome iOS with JavaScript, we can use the FileReader constructor.

Categories
JavaScript Answers Nodejs

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

Sometimes, we want to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

In this article, we’ll look at how to read a 3 bytes Buffer as an integer with JavaScript and Node.js.

How to read a 3 bytes Buffer as an integer with JavaScript and Node.js?

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

For instance, we write:

const buffer = Buffer.from('hello world', 'utf8');
const decimal = buffer.readUIntBE(0, 3);
console.log(decimal)

to create a buffer with Buffer.from.

Then we call buffer.readUIntBE with 0 and 3 to read the 3 bytes and convert it into a decimal number.

Therefore, decimal is 6841708.

Conclusion

To read a 3 bytes Buffer as an integer with JavaScript and Node.js, we can use the readUintBE method.

Categories
JavaScript Answers

How to generate a random number between 2 values to 2 decimals places in JavaScript?

Sometimes, we want to generate a random number between 2 values to 2 decimals places in JavaScript.

In this article, we’ll look at how to generate a random number between 2 values to 2 decimals places in JavaScript.

How to generate a random number between 2 values to 2 decimals places in JavaScript?

To generate a random number between 2 values to 2 decimals places in JavaScript, we can use the Math.floor and Math.random methods.

For instance, we write:

const precision = 100;
const randomNum = Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision);
console.log(randomNum)

to call Math.random to generate a number between 0 and 1.

Then we multiply the returned number by (10 * precision - 1 * precision) and add by 1 * precision to return the number in the range we want multiplied by precision.

Then we divide by (1 * precision) to return the number with the precision and range we want.

Therefore, randomNum should be a random number with 2 decimal places since precision is 100.

Conclusion

To generate a random number between 2 values to 2 decimals places in JavaScript, we can use the Math.floor and Math.random methods.

Categories
JavaScript Answers

How to convert an HTML table to an array in JavaScript?

Sometimes, we want to convert an HTML table to an array in JavaScript.

In this article, we’ll look at how to convert an HTML table to an array in JavaScript.

How to convert an HTML table to an array in JavaScript?

To convert an HTML table to an array in JavaScript, we can use the spread operator and some array methods.

For instance, we add a table with:

<table id="cartGrid">
  <thead>
    <tr>
      <th>Item Description</th>
      <th>Qty</th>
      <th>Unit Price</th>
      <th>Ext Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Old Lamp</td>
      <td>1</td>
      <td>107.00</td>
      <td>107.00</td>
    <tr>
      <td>Blue POst</td>
      <td>2</td>
      <td>7.00</td>
      <td>14.00</td>
  </tbody>
</table>

Then we write:

const table = document.querySelector('table')
const arr = [...table.rows].map(r => [...r.querySelectorAll('td, th')].map(td => td.textContent))
console.log(arr)

to get all the tr elements in a node list with table.rows.

Then we spread them into an array.

Next, we call map with a callback that select all the td’s or th’s, spread them into an array, and then call map on them to get their textContent.

As a result, arr is:

[
  [
    "Item Description",
    "Qty",
    "Unit Price",
    "Ext Price"
  ],
  [
    "Old Lamp",
    "1",
    "107.00",
    "107.00"
  ],
  [
    "Blue POst",
    "2",
    "7.00",
    "14.00"
  ]
]

Conclusion

To convert an HTML table to an array in JavaScript, we can use the spread operator and some array methods.