Categories
JavaScript Answers

How to get the second to last item of an array with JavaScript?

Sometimes, we want to get the second to last item of an array with JavaScript.

In this article, we’ll look at how to get the second to last item of an array with JavaScript.

How to get the second to last item of an array with JavaScript?

To get the second to last item of an array with JavaScript, we use the array at method.

For instance, we write

const arr = [1, 2, 3, 4];
const secondLast = arr.at(-2);

to call arr.at with -2 to return the 2nd last item of the arr array.

Therefore, secondLast is 3.

Conclusion

To get the second to last item of an array with JavaScript, we use the array at method.

Categories
JavaScript Answers

How to split string with white space with JavaScript?

Sometimes, we want to split string with white space with JavaScript.

In this article, we’ll look at how to split string with white space with JavaScript.

How to split string with white space with JavaScript?

To split string with white space with JavaScript, we use the string split method.

For instance, we write

const arr = w.split(/(\s+)/).filter((e) => e.trim().length > 0);

to call split with a regex that matches whitespaces.

Then we call filter with a function that returns the strings that has length bigger than 0 after trimming the starting and ending whitespaces with trim.

/(\s+)/ matches whitespaces in string w and use them as separators.

Conclusion

To split string with white space with JavaScript, we use the string split method.

Categories
JavaScript Answers

How to display HTML code in text with sweet-alert and JavaScript?

Sometimes, we want to display HTML code in text with sweet-alert and JavaScript.

In this article, we’ll look at how to display HTML code in text with sweet-alert and JavaScript.

How to display HTML code in text with sweet-alert and JavaScript?

To display HTML code in text with sweet-alert and JavaScript, we can use the Swal.fire method.

For instance, we write

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

to add the sweet-alert 2 script tag.

Then we write

Swal.fire({
  title: "<i>Title</i>",
  html: "Test: <b>test</b>",
  confirmButtonText: "<u>OK</u>",
});

to call Swal.fire with an object with the title, html content, and the confirmButtonText content of the alert box.

Conclusion

To display HTML code in text with sweet-alert and JavaScript, we can use the Swal.fire method.

Categories
JavaScript Answers

How to fix .flat() is not a function with JavaScript?

Sometimes, we want to fix .flat() is not a function with JavaScript.

In this article, we’ll look at how to fix .flat() is not a function with JavaScript.

How to fix .flat() is not a function with JavaScript?

To fix .flat() is not a function with JavaScript, we can use the array reduce method instead of flat.

For instance, we write

const flatArr = result.reduce((acc, curr) => acc.concat(curr), []);

to call reduce with a callback that calls acc.concat(curr) to return an array with all the arrays in result combined into one flattened array with concat.

The 2nd argument is an empty array so acc‘s initial value is an empty array.

Conclusion

To fix .flat() is not a function with JavaScript, we can use the array reduce method instead of flat.

Categories
JavaScript Answers

How to fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript?

Sometimes, we want to fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript.

In this article, we’ll look at how to fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript.

How to fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript?

To fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript, we add a catch block.

For instance, we write

router.get("/emailfetch", authCheck, async (req, res) => {
  try {
    let emailFetch = await gmaiLHelper.getEmails(
      req.user._doc.profileId,
      "/messages",
      req.user.accessToken
    );
    emailFetch = emailFetch.data;
    res.send(emailFetch);
  } catch (error) {
    res.status(error.response.status);
    return res.send(error.message);
  }
});

to call getEmails which returns a promise.

If the returned promise is rejected, then the code in the catch block is run.

In the catch block, we run code to return a promise with error data.

Conclusion

To fix UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block with JavaScript, we add a catch block.