Categories
JavaScript Answers

How to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify?

Sometimes, we want to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify.

In this article, we’ll look at how to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify.

How to reduce numbers’ significance when stringifying values with JavaScript JSON.stringify?

To reduce numbers’ significance when stringifying values with JavaScript JSON.stringify, we can call JSON.stringify with a function that rounds numbers to the number of decimal places we want.

For instance, we write:

const a = [0.123456789123456789]
const strA = JSON.stringify(a, (key, val) => {
  return typeof val === 'number' ? Number(val.toFixed(3)) : val;
})
console.log(strA)

to call JSON.stringify with a and a callback that checks if the val value being stringified if a number.

If it is, we call val.toFixed with 3 to round val to 3 decimal places.

Otherwise, we return val as is.

Therefore, strA is '[0.123]'.

Conclusion

To reduce numbers’ significance when stringifying values with JavaScript JSON.stringify, we can call JSON.stringify with a function that rounds numbers to the number of decimal places we want.

Categories
JavaScript Answers

How to print objects in Node.js?

Sometimes, we want to print objects in Node.js.

In this article, we’ll look at how to print objects in Node.js.

How to print objects in Node.js?

To print objects in Node.js, we can use the util.inspect method.

For instance, we write:

const util = require('util')
const obj = {
  a: 0,
  b: 0
}
console.log(util.inspect(obj, {
  depth: null
}));

We call util.inspect with obj and { depth: null } to print the whole obj object.

Therefore, we see { a: 0, b: 0 } logged.

Conclusion

To print objects in Node.js, we can use the util.inspect method.

Categories
JavaScript Answers

How to convert a string array to an array in JavaScript?

Sometimes, we want to convert a string array to an array in JavaScript.

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

How to convert a string array to an array in JavaScript?

To convert a string array to an array in JavaScript, we can use the JSON.parse method.

For instance, we write:

const services = '["service1", "service2", "service3"]'
const s = JSON.parse(services)
console.log(s)

We call JSON.parse with services to parse it into a JavaScript array.

Therefore, s is ['service1', 'service2', 'service3'].

Conclusion

To convert a string array to an array in JavaScript, we can use the JSON.parse method.

Categories
JavaScript Answers Vue Answers

How to fix newline character is not rendered correctly with Vue.js and JavaScript?

Sometimes, we want to fix newline character is not rendered correctly with Vue.js and JavaScript.

In this article, we’ll look at how to fix newline character is not rendered correctly with Vue.js and JavaScript.

How to fix newline character is not rendered correctly with Vue.js and JavaScript?

To fix newline character is not rendered correctly with Vue.js and JavaScript, we can set the white-space CSS property to pre.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and the app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <div style="white-space: pre;">{{ text }}</div>
  `,
  data: {
    text: 'Hello Vue.\nThis is a line of text.\nAnother line of text.\n'
  },
})

We render the newline characters in text by setting the white-space CSS property to pre.

Therefore, we get:

Hello Vue.
This is a line of text.
Another line of text.

displayed.

Conclusion

To fix newline character is not rendered correctly with Vue.js and JavaScript, we can set the white-space CSS property to pre.

Categories
JavaScript Answers

How to add an event for a one time click to a function with JavaScript?

Sometimes, we want to add an event for a one time click to a function with JavaScript.

In this article, we’ll look at how to add an event for a one time click to a function with JavaScript.

How to add an event for a one time click to a function with JavaScript?

To add an event for a one time click to a function with JavaScript, we can call addEventListener with the once option set to true.

For instance, we write:

<button>
  click me
</button>

to add a button.

Then we write:

const button = document.querySelector('button')
button.addEventListener("click", () => {
  console.log('clicked')
}, {
  once: true
});

to select the button with querySelector.

Then we call button.addEventListener with 'click' and a click event listener callback to add a click listener to the button.

The callback will only run once since once is set to true in the object in the 3rd argument.

Conclusion

To add an event for a one time click to a function with JavaScript, we can call addEventListener with the once option set to true.