Categories
JavaScript Answers

How to remove elements in an array using Lodash?

Sometimes, we want to remove elements in an array using Lodash.

In this article, we’ll look at how to remove elements in an array using Lodash.

How to remove elements in an array using Lodash?

To remove elements in an array using Lodash, we can use remove method.

For instance, we write:

const fruits = ['Apple', 'Banana', 'Orange', 'Grape']
_.remove(fruits, (fruit) => {
  return _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1
});
console.log(fruits)

We call remove with fruits and a callback that returns the condition for the items that should be removed.

We returned _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1 so 'Apple', 'Banana' and 'Orange' are removed from fruits.

The removed items are returned.

As a result, fruits is ['Grape'] after remove is called.

Conclusion

To remove elements in an array using Lodash, we can use remove method.

Categories
JavaScript Answers

How to get month name from two digit month number with JavaScript?

Sometimes, we want to get month name from two digit month number with JavaScript.

In this article, we’ll look at how to get month name from two digit month number with JavaScript.

How to get month name from two digit month number with JavaScript?

To get month name from two digit month number with JavaScript, we can use the moment.js’ format method.

For instance, we write:

const formattedMonth = moment().month(9).format('MMMM');
console.log(formattedMonth)

We call format with 'MMMM' to get the full month name.

As a result, formattedMonth is 'October' since we called month with 9 before calling format.

Conclusion

To get month name from two digit month number with JavaScript, we can use the moment.js’ format method.

Categories
JavaScript Answers

How to hide an HTML element by ID with JavaScript?

Sometimes, we want to hide an HTML element by ID with JavaScript.

In this article, we’ll look at how to hide an HTML element by ID with JavaScript.

How to hide an HTML element by ID with JavaScript?

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

For instance, we write:

<div class="nav">
  <ul>
    <li>
      <a id="nav-ask" href="/questions/ask">
        Ask Question
      </a>
    </li>
  </ul>
</div>

to add some elements.

Then we can hide the a element by writing:

const link = document.getElementById('nav-ask');
link.style.display = 'none';

or

const link = document.getElementById('nav-ask');
link.style.visibility = 'hidden';

Conclusion

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

Categories
JavaScript Answers

How to store compressed JSON data in local storage with JavaScript?

Sometimes, we want to store compressed JSON data in local storage with JavaScript.

In this article, we’ll look at how to store compressed JSON data in local storage with JavaScript.

How to store compressed JSON data in local storage with JavaScript?

To store compressed JSON data in local storage with JavaScript, we can use the lz-string library.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>

to add the library script.

Then we write:

const jsonobj = {
  'sample': 'This is supposed to be ling string',
  'score': 'another long string which is going to be compressed'
}

localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));

const string = LZString.decompress(localStorage.getItem('mystring'))
console.log(JSON.parse(string));

to call LZString.compress to compress the stringified version of jsonobj.

Then we decompress the string with LZString.decompress.

And then we call JSON.parse to parse the decompressed string.

Conclusion

To store compressed JSON data in local storage with JavaScript, we can use the lz-string library.

Categories
JavaScript Answers

How to programmatically use CSS transitions with JavaScript?

Sometimes, we want to programmatically use CSS transitions with JavaScript.

In this article, we’ll look at how to programmatically use CSS transitions with JavaScript.

How to programmatically use CSS transitions with JavaScript?

To programmatically use CSS transitions with JavaScript, we can use an element’s animate method.

For instance, we write:

<img src='https://www.macmillandictionary.com/external/slideshow/full/emoji_snowflake_full.jpg' style='width: 100px'>

to add an image to animate.

Then we write:

const snowFlake = document.querySelector('img')
const snowLeft = 200
const player = snowFlake.animate(
  [{
      transform: `translate(${snowLeft}px, -100%)'}`
    },
    {
      transform: `translate(${snowLeft}px, ${window.innerHeight}px)`
    }
  ],
  1500);

setTimeOut(() => {
  player.cancel();
}, 2000)

We select the image with document.querySelector.

Then we call snowFlake.animate with an array of key frames.

We set the transform property to move the image.

And we specify the duration to be 1500ms.

Finally, we call player.cancel to stop the animation after 2000ms.

Conclusion

To programmatically use CSS transitions with JavaScript, we can use an element’s animate method.