Categories
JavaScript Answers

How to add 1 month from now to current date in moment.js and JavaScript?

Sometimes, we want to add 1 month from now to current date in moment.js and JavaScript.

In this article, we’ll look at how to add 1 month from now to current date in moment.js and JavaScript.

How to add 1 month from now to current date in moment.js and JavaScript?

To add 1 month from now to current date in moment.js and JavaScript, we can use the add method.

For instance, we write

const startDate = "20-03-2022";
const newDate = moment(startDate, "DD-MM-YYYY").add(5, "days");

to call moment to parse startDate into a moment object.

Then we call add with 5 and 'days' to return a new moment object with date 5 days after startDate.

Conclusion

To add 1 month from now to current date in moment.js and JavaScript, we can use the add method.

Categories
JavaScript Answers

How to use JavaScript array map to filter results with if conditional?

Sometimes, we want to use JavaScript array map to filter results with if conditional.

In this article, we’ll look at how to use JavaScript array map to filter results with if conditional.

How to use JavaScript array map to filter results with if conditional?

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

For instance, we write

const appIds = applicationsHere
  .filter((obj) => {
    return obj.selected;
  })
  .map((obj) => {
    return obj.id;
  });

to call filter to return an array with all the items with selected set to true.

Then we call map with a callback to return the id property of each object and put them in a new array and return the array.

Conclusion

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

Categories
JavaScript Answers

How to count the number of elements with same class with JavaScript?

Sometimes, we want to count the number of elements with same class with JavaScript.

In this article, we’ll look at how to count the number of elements with same class with JavaScript.

How to count the number of elements with same class with JavaScript?

To count the number of elements with same class with JavaScript, we can use the querySelectorAll method.

For instance, we write

const count = document.querySelectorAll("#main-div .specific-class").length;

to call querySelectorAll with the selector for the elements with the specific-class class to get all the elements with that class in a node list.

Then we use the length property to get the count of the number of elements selected.

Conclusion

To count the number of elements with same class with JavaScript, we can use the querySelectorAll method.

Categories
Vue Answers

How to get the DOM element to corresponding Vue.js component with JavaScript?

Sometimes, we want to get the DOM element to corresponding Vue.js component with JavaScript.

In this article, we’ll look at how to get the DOM element to corresponding Vue.js component with JavaScript.

How to get the DOM element to corresponding Vue.js component with JavaScript?

To get the DOM element to corresponding Vue.js component with JavaScript, we can use refs.

For instance, we write

<template>
  <div>
    root element
    <div ref="childElement">child element</div>
  </div>
</template>

<script>
export default {
  mounted() {
    const rootElement = this.$el;
    const childElement = this.$refs.childElement;

    console.log(rootElement);
    console.log(childElement);
  },
};
</script>

to assign the childElement ref to the div.

Then in the mounted hook, we get the root element of the component with this.$el.

And we get the div with this.$refs.childElement.

Conclusion

To get the DOM element to corresponding Vue.js component with JavaScript, we can use refs.

Categories
JavaScript Answers

How to fetch an array of URLs with Promise.all with JavaScript?

Sometimes, we want to fetch an array of URLs with Promise.all with JavaScript.

In this article, we’ll look at how to fetch an array of URLs with Promise.all with JavaScript.

How to fetch an array of URLs with Promise.all with JavaScript?

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.

For instance, we write

const texts = await Promise.all(
  urls.map(async (url) => {
    const resp = await fetch(url);
    return resp.text();
  })
);

to call urls.map with an async function that calls fetch and returns the response as the resolve value of the promise returned.

Then we call Promise.all with the array of promises returned by map to make the requests.

Next we get the responses from the texts array which we get from using await on the promise returned by Promise.all.

Conclusion

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.