Categories
JavaScript Answers

How to add template literals with a forEach in it with JavaScript?

Sometimes, we want to add template literals with a forEach in it with JavaScript.

In this article, we’ll look at how to add template literals with a forEach in it with JavaScript.

How to add template literals with a forEach in it with JavaScript?

To add template literals with a forEach in it with JavaScript, we should replace forEach with map.

For instance, we write:

const arr = ['apple', 'orange', 'grape']
const options = arr
  .map(a => {
    return `<option value='${a}'>${a}</option>`
  })
  .join('')
const str = `<select>${options}</select>`
console.log(str)

to call arr.map with a callback that maps the values in arr to an option element string.

Then we call join to combine the strings from the array returned by map into a single string.

Next, we put the options string into the str template literal.

As a result, str is:

<select><option value='apple'>apple</option><option value='orange'>orange</option><option value='grape'>grape</option></select>

Conclusion

To add template literals with a forEach in it with JavaScript, we should replace forEach with map.

Categories
JavaScript Answers

How to fix the issue when we call JavaScript array push with multiple objects and gets undefined?

Sometimes, we want to fix the issue when we call JavaScript array push with multiple objects and gets undefined.

In this article, we’ll look at how to fix the issue when we call JavaScript array push with multiple objects and gets undefined.

How to fix the issue when we call JavaScript array push with multiple objects and gets undefined?

To fix the issue when we call JavaScript array push with multiple objects and gets undefined, we should get the latest value of the array we call push on.

For instance, we write:

const arr = [1, 2, 3]
arr.push(...[4, 5, 6])
console.log(arr)

to call arr.push with the [4, 5, 6] spread as the arguments of push with the spread operator.

Therefore, from the console log, arr is [1, 2, 3, 4, 5, 6] since push appends the values into arr in place.

Conclusion

To fix the issue when we call JavaScript array push with multiple objects and gets undefined, we should get the latest value of the array we call push on.

Categories
JavaScript Answers

How to get the same result as C# String.ToCharArray() with JavaScript?

Sometimes, we want to get the same result as C# String.ToCharArray() with JavaScript.

In this article, we’ll look at how to get the same result as C# String.ToCharArray() with JavaScript.

How to get the same result as C# String.ToCharArray() with JavaScript?

To get the same result as C# String.ToCharArray() with JavaScript, we can use the spread operator.

For instance, we write:

const str = "12345";
const strArr = [...str];
console.log(strArr)

to spread the str string into an array of characters with the spread operator.

Then we assign the returned array to strArr.

Therefore, strArr is  ['1', '2', '3', '4', '5'].

Conclusion

To get the same result as C# String.ToCharArray() with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to add 24 hours to datetime object of JavaScript?

Sometimes, we want to add 24 hours to datetime object of JavaScript.

In this article, we’ll look at how to add 24 hours to datetime object of JavaScript.

How to add 24 hours to datetime object of JavaScript?

To add 24 hours to datetime object of JavaScript, we can use getTime to get the timestamp of the original datetime.

And then we add 60 * 60 * 24 * 1000 to it to add 24 hours to it.

Then we can use the Date constructor with the timestamp to create a new datetime with the 24 hours added.

For instance, we write:

const d = new Date(2022, 1, 1, 1)
const newD = new Date(d.getTime() + 60 * 60 * 24 * 1000);
console.log(d)

to create the datetime d.

Then we call d.getTime to get timestamp from d in milliseconds.

Next, we add 60 * 60 * 24 * 1000 which is 24 hours in milliseconds to the timestamp.

And then we use the Date constructor with the return timestamp and assign it to newD.

As a result, newD is Tue Feb 01 2022 01:00:00 GMT-0800 (Pacific Standard Time).

Conclusion

To add 24 hours to datetime object of JavaScript, we can use getTime to get the timestamp of the original datetime.

And then we add 60 * 60 * 24 * 1000 to it to add 24 hours to it.

Then we can use the Date constructor with the timestamp to create a new datetime with the 24 hours added.

Categories
JavaScript Answers

How to reload a page every X seconds with JavaScript?

Sometimes, we want to reload a page every X seconds with JavaScript.

In this article, we’ll look at how to reload a page every X seconds with JavaScript.

How to reload a page every X seconds with JavaScript?

To reload a page every X seconds with JavaScript, we can use the setInterval function.

For instance, we write:

setInterval(() => {
  location.reload(true);
}, 5000);

to call setInterval with a function that calls location.reload to reload the page.

The 2nd argument is the interval to wait until the callback is called again.

Conclusion

To reload a page every X seconds with JavaScript, we can use the setInterval function.