Categories
JavaScript Answers

How to get yesterday’s date with moment.js and JavaScript?

Sometimes, we want to get yesterday’s date with moment.js and JavaScript.

In this article, we’ll look at how to get yesterday’s date with moment.js and JavaScript.

How to get yesterday’s date with moment.js and JavaScript?

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

For instance, we write

const yesterday = moment().subtract(1, "days");

to call moment to create a moment object with the current date.

And then we call subtract with 1 and 'days' to return a moment object that subtracts the current date by 1 day.

Conclusion

To get yesterday’s date with moment.js and JavaScript, we call the subtract method.

Categories
JavaScript Answers

How to clean up sinon stubs with JavaScript?

Sometimes, we want to clean up sinon stubs with JavaScript.

In this article, we’ll look at how to clean up sinon stubs with JavaScript.

How to clean up sinon stubs with JavaScript?

To clean up sinon stubs with JavaScript, we can call the sinon.restore method.

For instance, we write

const sinon = require("sinon");

it("should do something", () => {
  sinon.stub(some, "method");
});

afterEach(() => {
  sinon.restore();
});

to call sinon.stub to stub the some.method method in our test.

And then we call sinon.restore in the afterEach hook to clean up sinon stubs after each test.

Conclusion

To clean up sinon stubs with JavaScript, we can call the sinon.restore method.

Categories
JavaScript Answers

How to remove undefined fields from an object with JavaScript?

Sometimes, we want to remove undefined fields from an object with JavaScript

In this article, we’ll look at how to remove undefined fields from an object with JavaScript.

How to remove undefined fields from an object with JavaScript?

To remove undefined fields from an object with JavaScript, we can use the Object.keys method to get the keys of the object.

Then we call forEach with a callback that removes the undefined properties.

For instance, we write

Object.keys(obj).forEach((key) => {
  if (obj[key] === undefined) {
    delete obj[key];
  }
});

to get the obj object’s keys with Object.keys.

Then we call forEach with a callback to check if each object property is undefined with

obj[key] === undefined

And if it’s true, we use delete to remove the property.

Conclusion

To remove undefined fields from an object with JavaScript, we can use the Object.keys method to get the keys of the object.

Then we call forEach with a callback that removes the undefined properties.

Categories
JavaScript Answers

How to get a number of random elements from an array with JavaScript?

Sometimes, we want to get a number of random elements from an array with JavaScript.

In this article, we’ll look at how to get a number of random elements from an array with JavaScript.

How to get a number of random elements from an array with JavaScript?

To get a number of random elements from an array with JavaScript, we call array sort to shuffle the array.

Then we get a subarray from the shuffled array with slice.

For instance, we write

const shuffled = array.sort(() => 0.5 - Math.random());
const selected = shuffled.slice(0, n);

to call array.sort with a callback that returns a number between -0.5 and 0.5 to return a shuffled version of array.

And then we call shuffled.slice with 0 and n to get the first n elements.

Conclusion

To get a number of random elements from an array with JavaScript, we call array sort to shuffle the array.

Then we get a subarray from the shuffled array with slice.

Categories
JavaScript Answers

How to change the value of a global variable inside of a function with JavaScript?

Sometimes, we want to change the value of a global variable inside of a function with JavaScript.

In this article, we’ll look at how to change the value of a global variable inside of a function with JavaScript.

How to change the value of a global variable inside of a function with JavaScript?

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.

For instance, we write

a = 10;

const myFunction = () => {
  a = 20;
};

myFunction();

to set the global variable a to 20 in the myFunction function.

Conclusion

To change the value of a global variable inside of a function with JavaScript, we can assign the global variable to a new value.