Categories
JavaScript Answers

How to convert CSV to JSON in Node.js and JavaScript?

Sometimes, we want to convert CSV to JSON in Node.js and JavaScript.

In this article, we’ll look at how to convert CSV to JSON in Node.js and JavaScript.

How to convert CSV to JSON in Node.js and JavaScript?

To convert CSV to JSON in Node.js and JavaScript, we use the csvtojson library.

To install it, we run

npm i csvtojson

Then we use it by writing

const csv = require("csvtojson");

const csvFilePath = "customer-data.csv";
const jsonObj = await csv().fromFile(csvFilePath);
console.log(jsonObj);

to call fromFile with the csvFilePath to read the csv file and return a promise with the csv data parsed into a JavaScript object.

And then we use await to get the data from the returned promise.

Conclusion

To convert CSV to JSON in Node.js and JavaScript, we use the csvtojson library.

Categories
JavaScript Answers

How to call an asynchronous function within map with JavaScript?

Sometimes, we want to call an asynchronous function within map with JavaScript.

In this article, we’ll look at how to call an asynchronous function within map with JavaScript.

How to call an asynchronous function within map with JavaScript?

To call an asynchronous function within map with JavaScript, we can use the Promise.all method.

For instance, we write

const data = await Promise.all(
  data.map(async (item) => {
    try {
      item.fetchItem = await fetchFunc(item.fetchParams);

      return item;
    } catch (err) {
      throw err;
    }
  })
);

to call data.map with an async function as its callback.

The callback’s return statement returns the resolve value of the promise returned by the async function.

Then we call Promise.all on the returned array of promises to return a promise with an array of resolve value of each async function.

And then we use await to get the array of resolve values.

Conclusion

To call an asynchronous function within map with JavaScript, we can use the Promise.all method.

Categories
CSS

How to force website to show in landscape mode only with CSS?

Sometimes, we want to force website to show in landscape mode only with CSS.

In this article, we’ll look at how to force website to show in landscape mode only with CSS.

How to force website to show in landscape mode only with CSS?

To force website to show in landscape mode only with CSS, we can set the width of the page in different orientations.

For instance, we write

@media only screen and (orientation: portrait) {
  #wrapper {
    width: 1024px;
  }
}

@media only screen and (orientation: landscape) {
  #wrapper {
    width: 1024px;
  }
}

to set the element with ID wrapper to width 1024px in portrait and landscape orientation.

Conclusion

To force website to show in landscape mode only with CSS, we can set the width of the page in different orientations.

Categories
JavaScript Answers

How to fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript?

Sometimes, we want to fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript.

In this article, we’ll look at how to fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript.

How to fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript?

To fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript, we need to import the Firestore modules.

For instance, we write

import firebase from "firebase/app";
import "firebase/firestore";

to imoort firebase and firebase/firestore to import the required dependencies.

Conclusion

To fix firebase.firestore() is not a function when trying to initialize Cloud Firestore with JavaScript, we need to import the Firestore modules.

Categories
JavaScript Answers

How to check for keyCode values for numeric keypad with JavaScript?

Sometimes, we want to check for keyCode values for numeric keypad with JavaScript.

In this article, we’ll look at how to check for keyCode values for numeric keypad with JavaScript.

How to check for keyCode values for numeric keypad with JavaScript?

To check for keyCode values for numeric keypad with JavaScript, we can check for the key code ranges for numeric keys.

For instance, we write

if (
  (e.keyCode >= 48 && e.keyCode <= 57) ||
  (e.keyCode >= 96 && e.keyCode <= 105)
) {
  // ...
}

to check for presses for the digit keys.

The digit keys on the top of the keyboard has key code range 48 to 57.

The digit keys on the numeric keypad has key code range 96 and 105.

Conclusion

To check for keyCode values for numeric keypad with JavaScript, we can check for the key code ranges for numeric keys.