Categories
JavaScript Answers

How to fix JSON.stringify changes time of date because of UTC with JavaScript?

Sometimes, we want to fix JSON.stringify changes time of date because of UTC with JavaScript.

In this article, we’ll look at how to fix JSON.stringify changes time of date because of UTC with JavaScript.

How to fix JSON.stringify changes time of date because of UTC with JavaScript?

To fix JSON.stringify changes time of date because of UTC with JavaScript, we can use JSON.parse to convert the date back to its original format.

For instance, we write

let currentDate = new Date();
currentDate = JSON.stringify(currentDate);
currentDate = new Date(JSON.parse(currentDate));

to call create the currentDate date object.

Then we call JSON.stringify to convert the currentDate object into a JSON string.

Then we call JSON.parse to parse the date string back to its original format.

And then we use the Date constructor to convert it back to a date object.

Conclusion

To fix JSON.stringify changes time of date because of UTC with JavaScript, we can use JSON.parse to convert the date back to its original format.

Categories
JavaScript Answers

How to get the containing form of an input with JavaScript?

Sometimes, we want to get the containing form of an input with JavaScript.

In this article, we’ll look at how to get the containing form of an input with JavaScript.

How to get the containing form of an input with JavaScript?

To get the containing form of an input with JavaScript, we can use the form property.

For instance, we write

const form = inputEl.form;

to get the form element that inputEl is in with the inputEl.form property.

Conclusion

To get the containing form of an input with JavaScript, we can use the form property.

Categories
JavaScript Answers

How to filter array by multiple conditions with JavaScript?

Sometimes, we want to filter array by multiple conditions with JavaScript.

In this article, we’ll look at how to filter array by multiple conditions with JavaScript.

How to filter array by multiple conditions with JavaScript?

To filter array by multiple conditions with JavaScript, we can use the array filter method.

For instance, we write

const filter = {
  address: "England",
  name: "Mark",
};
const users = [
  {
    name: "John",
    email: "johnson@mail.com",
    age: 25,
    address: "USA",
  },
  {
    name: "Tom",
    email: "tom@mail.com",
    age: 35,
    address: "England",
  },
  {
    name: "Mark",
    email: "mark@mail.com",
    age: 28,
    address: "England",
  },
];

const filteredUsers = users.filter((item) => {
  for (const key of Object.keys(filter)) {
    if (item[key] === undefined || item[key] !== filter[key]) {
      return false;
    }
  }
  return true;
});

console.log(users);

to call users.filter with a callback.

In the callback, we loop through the filter object’s keys we get from Object.keys.

In the loop, we check the value of each filter value to see if they’re what we’re looking for.

If they’re not, we return false.

Otherwise, we return true to include it in the filtered array returned.

Conclusion

To filter array by multiple conditions with JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to increment letters with JavaScript?

Sometimes, we want to increment letters with JavaScript.

In this article, we’ll look at how to increment letters with JavaScript.

How to increment letters with JavaScript?

To increment letters with JavaScript, we can use the String.fromCharCode method.

For instance, we write

const c = String.fromCharCode("A".charCodeAt() + 1);

to call String.fromCharCode with "A".charCodeAt() + 1 to return 'B'.

We get the character code of 'A' with charCodeAt.

Then we add 1 to it and use the sum as the argument of fromCharCode to get the letter after 'A'.

Conclusion

To increment letters with JavaScript, we can use the String.fromCharCode method.

Categories
JavaScript Answers

How to convert blob from DataURL with JavaScript?

Sometimes, we want to convert blob from DataURL with JavaScript.

In this article, we’ll look at how to convert blob from DataURL with JavaScript.

How to convert blob from DataURL with JavaScript?

To convert blob from DataURL with JavaScript, we can use fetch.

For instance, we write

const url =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

const res = await fetch(url);
const blob = await res.blob();

to call fetch with url to return a promise with the response.

Then we call res.blob to return a promise with the blob from the response object.

Conclusion

To convert blob from DataURL with JavaScript, we can use fetch.