Categories
JavaScript Answers

How to sort arrays numerically by object property value with JavaScript?

Sometimes, we want to sort arrays numerically by object property value with JavaScript.

In this article, we’ll look at how to sort arrays numerically by object property value with JavaScript.

How to sort arrays numerically by object property value with JavaScript?

To sort arrays numerically by object property value with JavaScript, we can use the array sort method.

For instance, we write

const sorted = myArray.sort((a, b) => a.distance - b.distance);

to call myArray.sort with a callback that returns a.distance - b.distance to return a new array that sort the entries in myArray by the value of the distance property of each object in ascending order.

Conclusion

To sort arrays numerically by object property value with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to declare an empty two-dimensional array in JavaScript?

Sometimes, we want to declare an empty two-dimensional array in JavaScript.

In this article, we’ll look at how to declare an empty two-dimensional array in JavaScript.

How to declare an empty two-dimensional array in JavaScript?

To declare an empty two-dimensional array in JavaScript, we can use the array fill method.

For instance, we write

const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));

to call Array with 5 to create an array with 5 empty slots.

Then we call fill to fill the slots with undefined.

Next we call map with a callback to return an array with 4 empty slots and fill them with 0 with fill.

Conclusion

To declare an empty two-dimensional array in JavaScript, we can use the array fill method.

Categories
JavaScript Answers

How to generate array of random dates within two dates with JavaScript?

Sometimes, we want to generate array of random dates within two dates with JavaScript.

In this article, we’ll look at how to generate array of random dates within two dates with JavaScript.

How to generate array of random dates within two dates with JavaScript?

To generate array of random dates within two dates with JavaScript, we can use the Math.random method.

For instance, we write

const randomDate = (start, end) => {
  return new Date(
    start.getTime() + Math.random() * (end.getTime() - start.getTime())
  );
};

const d = randomDate(new Date(2012, 0, 1), new Date());
console.log(d);

to define the randomDate function that takes the start and end dates.

We get the timestamp in between start and end with

start.getTime() + Math.random() * (end.getTime() - start.getTime())

Math.random returns a number between 0 and 1, so we multiply the returned number with (end.getTime() - start.getTime()) and add start.getTime to get a timestamp in milliseconds between start and end and use that as the argument of Date to get a new Date object with the timestamp.

Conclusion

To generate array of random dates within two dates with JavaScript, we can use the Math.random method.

Categories
JavaScript Answers

How to get tomorrow’s date in format dd-mm-yy with JavaScript?

Sometimes, we want to get tomorrow’s date in format dd-mm-yy with JavaScript.

In this article, we’ll look at how to get tomorrow’s date in format dd-mm-yy with JavaScript.

How to get tomorrow’s date in format dd-mm-yy with JavaScript?

To get tomorrow’s date in format dd-mm-yy with JavaScript, we can use the date instance’s toLocaleDateString method.

For instance, we write

const d = new Date(2022, 1, 29);
console.log(d.toLocaleDateString());

to create date d with the Date constructor.

Then we call toLocaleDateString to return a human readable date string.

Conclusion

To get tomorrow’s date in format dd-mm-yy with JavaScript, we can use the date instance’s toLocaleDateString method.

Categories
JavaScript Answers

How to reprompt for permissions with getUserMedia() after initial denial with JavaScript?

Sometimes, we want to reprompt for permissions with getUserMedia() after initial denial with JavaScript.

In this article, we’ll look at how to reprompt for permissions with getUserMedia() after initial denial with JavaScript.

How to reprompt for permissions with getUserMedia() after initial denial with JavaScript?

To reprompt for permissions with getUserMedia() after initial denial with JavaScript, we can call the navigator.permissions.query method.

For instance, we write

const permissionObj = await navigator.permissions.query({ name: "microphone" });

to call navigator.permissions.query in an async function with an object with the name property set to a string with the permission that we want get.

The user will then be prompted with the permission prompt and permissionObj will have the user’s choice once the prompt is closed.

query returns a promise so we use await to get permissionObj.

Conclusion

To reprompt for permissions with getUserMedia() after initial denial with JavaScript, we can call the navigator.permissions.query method.