Categories
JavaScript Answers

How to get the week number of the month of a given date with JavaScript?

Sometimes, we want to get the week number of the month of a given date with JavaScript.

In this article, we’ll look at how to get the week number of the month of a given date with JavaScript.

How to get the week number of the month of a given date with JavaScript?

To get the week number of the month of a given date with JavaScript, we can calculate it by getting the first date of the month of the given date.

Then we add the day of the month of the given date plus the first day of the month minus 1, divide everything by 7 and take the ceiling of the whole expression.

For instance, we write:

const getWeekNumOfMonthOfDate = (d) => {
  const firstDay = new Date(d.getFullYear(), d.getMonth(), 1).getDay();
  return Math.ceil((d.getDate() + (firstDay - 1)) / 7);
}

const weekNumOfDate = getWeekNumOfMonthOfDate(new Date(2022, 1, 20))
console.log(weekNumOfDate)

to define the getWeekNumOfMonthOfDate function to do the calculation.

We get the first day of the month which is given by date d with:

const firstDay = new Date(d.getFullYear(), d.getMonth(), 1).getDay()

Then we add the date of the month with (d.getDate() + (firstDay - 1)).

And then we get the week number by dividing by 7.

Finally, we take the ceiling of the with Math.ceil to round that up to a whole number.

Therefore, the console log should log 3 since Feb 20, 2022 is in the 3rd week of February, 2022.

Conclusion

To get the week number of the month of a given date with JavaScript, we can calculate it by getting the first date of the month of the given date.

Then we add the day of the month of the given date plus the first day of the month minus 1, divide everything by 7 and take the ceiling of the whole expression.

Categories
JavaScript Answers

How to use Promise.race() with JavaScript?

Sometimes, we want to use Promise.race() with JavaScript.

In this article, we’ll look at how to use Promise.race() with JavaScript.

How to use Promise.race() with JavaScript?

To use Promise.race() with JavaScript, we can call it with an array of promises.

For instance, we write:

const p1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 500, 'one');
});
const p2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'two');
});

(async () => {
  const value = await Promise.race([p1, p2])
  console.log(value);
})()

We call Promise.race with [p1, p2] which will return the promise that is settled first.

Therefore, value should be 'two' since the setTimeout callback is run in 100 seconds after the callback is queued for running.

Conclusion

To use Promise.race() with JavaScript, we can call it with an array of promises.

Categories
JavaScript Answers

How to reset a GIF animation with JavaScript?

Sometimes, we want to reset a GIF animation with JavaScript.

In this article, we’ll look at how to reset a GIF animation with JavaScript.

How to reset a GIF animation with JavaScript?

To reset a GIF animation with JavaScript, we can add a random query string to the end of the image URL.

For instance, we write:

<img src='https://media4.giphy.com/avatars/JulietGlock/S2TkRZ9GyF91.GIF' style='height: 100px'>

to add an animate GIF img element.

Then we write:

const img = document.querySelector('img')
setTimeout(() => {
  img.src = `${img.src.replace(/\?.*$/,"")}?x=${Math.random()}`;
}, 2000)

to select the img element with querySelector.

Then we set the img.src property to the img.src plus a new query string that had the x parameter set to a random number.

We have that in the setTimeout callback so src will be changed with a delay.

The animated GIF will start from the beginning when src is changed.

Conclusion

To reset a GIF animation with JavaScript, we can add a random query string to the end of the image URL.

Categories
JavaScript Answers

How to get a list of duplicate objects in an array of objects with JavaScript?

Sometimes, we want to get a list of duplicate objects in an array of objects with JavaScript.

In this article, we’ll look at how to get a list of duplicate objects in an array of objects with JavaScript.

How to get a list of duplicate objects in an array of objects with JavaScript?

To get a list of duplicate objects in an array of objects with JavaScript, we can use the JavaScript array’s reduce method.

For instance, we write:

const values = [{
  id: 10,
  name: 'someName1'
}, {
  id: 10,
  name: 'someName2'
}, {
  id: 11,
  name: 'someName3'
}, {
  id: 12,
  name: 'someName4'
}];

const lookup = values.reduce((a, e) => {
  if (a[e.id]) {
    return {
      ...a,
      [e.id]: a[e.id] + 1
    }
  }
  return {
    ...a,
    [e.id]: 1
  }
}, {});

const dups = values.filter(e => lookup[e.id] > 1)
console.log(dups);

We call values.reduce with a callback that returns an object with the counts of the object give in values given the value of e.id.

If a[e.id] is defined, then we increment it by 1.

Otherwise, we set [e.id] to 1.

Then we get the id‘s of the objects in values with value bigger than 1.

As a result, dups is:

[
  {
    "id": 10,
    "name": "someName1"
  },
  {
    "id": 10,
    "name": "someName2"
  }
]

Conclusion

To get a list of duplicate objects in an array of objects with JavaScript, we can use the JavaScript array’s reduce method.

Categories
JavaScript Answers

How to insert or remove HTML content between div tags with JavaScript?

Sometimes, we want to insert or remove HTML content between div tags with JavaScript.

In this article, we’ll look at how to insert or remove HTML content between div tags with JavaScript.

How to insert or remove HTML content between div tags with JavaScript?

To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML property of the element.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<span>Something</span>';

to select the div with querySelector.

Then we set div.innerHTML to a string with a span to replace ‘hello world’ with the span.

Therefore, we should see ‘Something’ instead of ‘hello world’ displayed.

Conclusion

To insert or remove HTML content between div tags with JavaScript, we can set the innerHTML property of the element.