Categories
JavaScript Answers

How to convert JavaScript object with numeric keys into array?

Sometimes, we want to convert JavaScript object with numeric keys into array.

In this article, we’ll look at how to convert JavaScript object with numeric keys into array.

How to convert JavaScript object with numeric keys into array?

To convert JavaScript object with numeric keys into array, we can use the array map method with Object.keys.

For instance, we write

const arr = Object.keys(obj).map((k) => {
  return obj[k];
});

to call Object.keys with obj to return the an array of keys in obj.

Then we call map with a callback to return the value of property k in obj.

Conclusion

To convert JavaScript object with numeric keys into array, we can use the array map method with Object.keys.

Categories
JavaScript Answers

How to use Jasmine to spy on a function without an object?

Sometimes, we want to use Jasmine to spy on a function without an object.

In this article, we’ll look at how to use Jasmine to spy on a function without an object.

How to use Jasmine to spy on a function without an object?

To use Jasmine to spy on a function without an object, we can use the spyOn function.

For instance, we write

import * as FooFunctions from "../foo_functions";

x = FooFunctions.foo(y);

to call foo in FooFunctions in the test file.

Then in the test, we write

spyOn(FooFunctions, "foo").and.callFake(() => {
  //...
});
// ...
expect(FooFunctions.foo).toHaveBeenCalled();

to call spyOn with the FooFunctions module and the 'foo' function name string.

And we call callFake with a function that we mock foo with.

And then we call expect to check if foo is called with toHaveBeenCalled.

Conclusion

To use Jasmine to spy on a function without an object, we can use the spyOn function.

Categories
JavaScript Answers

How to use setInterval and clearInterval with JavaScript?

Sometimes, we want to use setInterval and clearInterval with JavaScript.

In this article, we’ll look at how to use setInterval and clearInterval with JavaScript.

How to use setInterval and clearInterval with JavaScript?

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

For instance, we write

const handle = setInterval(drawAll, 20);

//...

clearInterval(handle);

to call setInterval with the drawAll function and 20 milliseconds to call drawAll every 20 milliseconds.

And then we call clearInterval with the handle to clear the timer returned by setInterval.

Conclusion

To use setInterval and clearInterval with JavaScript, we call clearInterval to clear the repeating timer with the timer ID which is returned by setInterval.

Categories
JavaScript Answers

How to fix JavaScript date.getYear() returning 111 in 2011?

Sometimes, we want to fix JavaScript date.getYear() returning 111 in 2011.

In this article, we’ll look at how to fix JavaScript date.getYear() returning 111 in 2011.

How to fix JavaScript date.getYear() returning 111 in 2011?

To fix JavaScript date.getYear() returning 111 in 2011, we replace getYear with getFullYear.

For instance, we write

const y = new Date().getFullYear();

to get the 4 digit year of the current date with getFullYear.

Conclusion

To fix JavaScript date.getYear() returning 111 in 2011, we replace getYear with getFullYear.

Categories
JavaScript Answers

How to return index of greatest value in an array with JavaScript?

Sometimes, we want to return index of greatest value in an array with JavaScript.

In this article, we’ll look at how to return index of greatest value in an array with JavaScript.

How to return index of greatest value in an array with JavaScript?

To return index of greatest value in an array with JavaScript, we can use the Math.max method and the array indexOf method.

For instance, we write

const i = arr.indexOf(Math.max(...arr));

to call Math.max with the arguments of arr spread as the arguments of max.

Then we call indexOf on the max number in arr returned by Math.max to get the index of the greatest number in arr.

Conclusion

To return index of greatest value in an array with JavaScript, we can use the Math.max method and the array indexOf method.