Categories
JavaScript Answers

How to throw exception from async function with JavaScript?

Sometimes, we want to throw exception from async function with JavaScript.

In this article, we’ll look at how to throw exception from async function with JavaScript.

How to throw exception from async function with JavaScript?

To throw exception from async function with JavaScript, we can use the throw keyword.

For instance, we write:

(async () => {
  try {
    await Promise.reject('error')
  } catch (e) {
    throw e;
  }
})()

We use the throw keyword to throw the error object e.

And we’ll see the error logged in the console.

Conclusion

To throw exception from async function with JavaScript, we can use the throw keyword.

Categories
JavaScript Answers

How to fix body data not sent with a Axios request with JavaScript?

Sometimes, we want to fix body data not sent with a Axios request with JavaScript.

In this article, we’ll look at how to fix body data not sent with a Axios request with JavaScript.

How to fix body data not sent with a Axios request with JavaScript?

To fix body data not sent with a Axios request with JavaScript, we can call axios.request with an object with the data property set to an object with the data.

For instance, we write:

axios.request({
  method: 'POST',
  url: `https://jsonplaceholder.typicode.com/posts`,
  headers: {
    'Authorization': 'token'
  },
  data: {
    title: 'abc'
  },

})

to call axios.request with an object with the data property set to the JSON object request payload.

The header property has the request header data.

Conclusion

To fix body data not sent with a Axios request with JavaScript, we can call axios.request with an object with the data property set to an object with the data.

Categories
JavaScript Answers

How to get the paragraph of selected text in web page with JavaScript?

Sometimes, we want to get the paragraph of selected text in web page with JavaScript.

In this article, we’ll look at how to get the paragraph of selected text in web page with JavaScript.

How to get the paragraph of selected text in web page with JavaScript?

To get the paragraph of selected text in web page with JavaScript, we can use the window.getSelection method.

For instance, we write:

<p>
  hello world
</p>
<button>
  get selection
</button>

to add a p and button elements.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  const x = window.getSelection()
  const z = x.anchorNode.parentNode
  console.log(z.innerHTML)
}

to select the button with querySelector.

And then we set button.onclick to a function that gets the selection from the page.

And we get the node with the selected content with x.anchorNode.parentNode.

Finally, we get the innerHTML from the node with the selected text.

Conclusion

To get the paragraph of selected text in web page with JavaScript, we can use the window.getSelection method.

Categories
JavaScript Answers

How to hide mouse cursor when mouse is within HTML5 canvas with JavaScript?

Sometimes, we want to hide mouse cursor when mouse is within HTML5 canvas with JavaScript.

In this article, we’ll look at how to hide mouse cursor when mouse is within HTML5 canvas with JavaScript.

How to hide mouse cursor when mouse is within HTML5 canvas with JavaScript?

To hide mouse cursor when mouse is within HTML5 canvas with JavaScript, we can set the cursor CSS property to 'none'.

For instance, we write:

<canvas width='200' height='200'></canvas>

to add a canvas.

Then we write:

document.querySelector('canvas').style.cursor = "none";

to select a canvas with querySelector.

And then we set the canvas’ style.cursor to 'none' to hide the cursor when the mouse is in the canvas.

Conclusion

To hide mouse cursor when mouse is within HTML5 canvas with JavaScript, we can set the cursor CSS property to 'none'.

Categories
JavaScript Answers

How to map an array with duplicate values to a unique array in JavaScript?

Sometimes, we want to map an array with duplicate values to a unique array in JavaScript.

In this article, we’ll look at how to map an array with duplicate values to a unique array in JavaScript.

How to map an array with duplicate values to a unique array in JavaScript?

To map an array with duplicate values to a unique array in JavaScript, we can use some array methods.

For instance, we write:

const data = [{
    "topicId": 1,
    "subTopicId": 1,
    "topicName": "a",
    "subTopicName1": "w"
  },
  {
    "topicId": 2,
    "subTopicId": 2,
    "topicName": "b",
    "subTopicName2": "x"
  },
  {
    "topicId": 3,
    "subTopicId": 3,
    "topicName": "c",
    "subTopicName3": "y"
  },
  {
    "topicId": 1,
    "subTopicId": 4,
    "topicName": "c",
    "subTopicName4": "z"
  }
];

const noDups = [...new Set(data.map(d => d.topicId))].map(topicId => {
  return data.find(d => d.topicId === topicId)
})
console.log(noDups)

We create a new set with the topicIds with the duplicates removed and spread that back to an array.

Then we call map with a callback to return the first item with the given topicId in data.

Therefore, noDups is:

[
  {
    "topicId": 1,
    "subTopicId": 1,
    "topicName": "a",
    "subTopicName1": "w"
  },
  {
    "topicId": 2,
    "subTopicId": 2,
    "topicName": "b",
    "subTopicName2": "x"
  },
  {
    "topicId": 3,
    "subTopicId": 3,
    "topicName": "c",
    "subTopicName3": "y"
  }
]

Conclusion

To map an array with duplicate values to a unique array in JavaScript, we can use some array methods.