Categories
JavaScript Answers

How to convert JSON object to CSV format in JavaScript?

Sometimes, we want to convert JSON object to CSV format in JavaScript.

In this article, we’ll look at how to convert JSON object to CSV format in JavaScript.

How to convert JSON object to CSV format in JavaScript>

To convert JSON object to CSV format in JavaScript, we use the some object and array methods.

For instance, we write

const convertToCSV = (arr) => {
  const array = [Object.keys(arr[0])].concat(arr);

  return array
    .map((it) => {
      return Object.values(it).toString();
    })
    .join("\n");
};

console.log(
  convertToCSV([
    {
      id: 1,
      name: "Foo",
      timestamp: new Date(),
    },
    {
      id: 2,
      name: "Bar",
      timestamp: new Date(),
    },
    {
      id: 3,
      name: "Baz",
      timestamp: new Date(),
    },
  ])
);

to define the convertToCSV function.

In it, we get the keys from the first object in the arr array with Object.keys(arr[0]).

Then we put the keys array in an array and call concat with arr.

Next, we call array.map to return the values of the object and return an array of array with the values of each row.

We call toString to return a commas separated string with the values.

And then we call join with '\n' to join the rows together with newline.

Conclusion

To convert JSON object to CSV format in JavaScript, we use the some object and array methods.

Categories
JavaScript Answers

How to change image size with JavaScript?

Sometimes, we want to change image size with JavaScript

In this article, we’ll look at how to change image size with JavaScript.

How to change image size with JavaScript?

To change image size with JavaScript, we set its width and height properties.

For instance, we write

<img src="https://picsum.photos/200/300" id="yourImgId" />

to add an img element.

Then we write

const image = document.getElementById("yourImgId");
if (image?.style) {
  image.style.height = "100px";
  image.style.width = "200px";
}

to select the img element with getElementById.

Then we set its style.width and style.height properties to set its width and height respectively.

Conclusion

To change image size with JavaScript, we set its width and height properties.

Categories
JavaScript Answers

How to set button text via JavaScript?

Sometimes, we want to set button text via JavaScript.

In this article, we’ll look at how to set button text via JavaScript.

How to set button text via JavaScript?

To set button text via JavaScript, we set the textContent property.

For instance, we write

const b = document.createElement("button");
b.textContent = "test value";

const wrapper = document.getElementById("divWrapper");
wrapper.appendChild(b);

to create a button with createElement.

The we set its text by setting the textContent property.

Then we get the parent of the button with getElementById.

And finally we call wrapper.appendChild with b to add the button as the last child of the wrapper.

Conclusion

To set button text via JavaScript, we set the textContent property.

Categories
JavaScript Answers

How to disable scrolling in an iPhone web application with JavaScript?

Sometimes, we want to disable scrolling in an iPhone web application with JavaScript.

In this article, we’ll look at how to disable scrolling in an iPhone web application with JavaScript.

How to disable scrolling in an iPhone web application with JavaScript?

To disable scrolling in an iPhone web application with JavaScript, we prevent the default behavior when we touch the screen.

For instance, we write

document.addEventListener("touchstart", (e) => {
  e.preventDefault();
});

to listen to the touchstart event on the document with addEventListener.

In the event listener, we call e.preventDefault to stop scrolling when we touch the screen.

Conclusion

To disable scrolling in an iPhone web application with JavaScript, we prevent the default behavior when we touch the screen.

Categories
JavaScript Answers

How to compare dates with the isSame() function in moment.js and JavaScript?

Sometimes, we want to compare dates with the isSame() function in moment.js and JavaScript.

In this article, we’ll look at how to compare dates with the isSame() function in moment.js and JavaScript.

How to compare dates with the isSame() function in moment.js and JavaScript?

To compare dates with the isSame() function in moment.js and JavaScript, we call isSame with another moment object.

For instance, we write

const x = moment("28-02-2022", "DD-MM-YYYY");
const isSame = x.isSame(moment("28-02-2022", "DD-MM-YYYY"));

to call isSame with a moment object created with moment to check if it’s the same as x.

It should return true since x is the same as what we called isSame with.

Conclusion

To compare dates with the isSame() function in moment.js and JavaScript, we call isSame with another moment object.