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.

Categories
JavaScript Answers

How to get the index from a JSON object with value with JavaScript?

Sometimes, we want to get the index from a JSON object with value with JavaScript.

In this article, we’ll look at how to get the index from a JSON object with value with JavaScript.

How to get the index from a JSON object with value with JavaScript?

To get the index from a JSON object with value with JavaScript, we can use the array findIndex method.

For instance, we write:

const data = [{
  "name": "allFit",
  "section": "left"
}, {
  "name": "allbMatches",
  "section": "left"
}, {
  "name": "allOffers",
  "section": "left"
}, {
  "name": "allInterests",
  "section": "left"
}, {
  "name": "allResponses",
  "section": "left"
}, {
  "name": "divChanged",
  "section": "right"
}];

const val = "allInterests"
const index = data.findIndex((item) => {
  return item.name === val
});

console.log(index);

to call data.findIndex with a callback that finds the first item where the name property of an object in data is equal to val and returns the index of that object.

Therefore, index is 3 since the 4th object in data has name set to 'allInterests'.

Conclusion

To get the index from a JSON object with value with JavaScript, we can use the array findIndex method.

Categories
JavaScript Answers

How to retrieve file names out of a multi-file upload control with JavaScript?

Sometimes, we want to retrieve file names out of a multi-file upload control with JavaScript.

In this article, we’ll look at how to retrieve file names out of a multi-file upload control with JavaScript.

How to retrieve file names out of a multi-file upload control with JavaScript?

To retrieve file names out of a multi-file upload control with JavaScript, we can get them from the change event’s files property.

For instance, we write:

<input type='file' multiple>

to add a file input that accepts multiple files.

Next, we write:

const input = document.querySelector("input");
input.onchange = (e) => {
  for (const f of e.target.files) {
    console.log(f.name)
  }
}

to select the file input with querySelector.

Then we set the input.onchange property to add a change event handler to the file input.

In it, we loop through the selected files with e.target.files with the for-of loop.

And then we get the file name of the selected file with f.name.

Conclusion

To retrieve file names out of a multi-file upload control with JavaScript, we can get them from the change event’s files property.