Categories
JavaScript Answers

How to watch for array changes with JavaScript?

Spread the love

Sometimes, we want to watch for array changes with JavaScript.

In this article, we’ll look at how to watch for array changes with JavaScript.

How to watch for array changes with JavaScript?

To watch for array changes with JavaScript, we use the underscore-observe library.

We use it by installing Lodash or Underscore.

Then we add contents of the https://github.com/mennovanslooten/underscore-observe/blob/master/underscore-observe.js file into our code.

Then we watch an array for changes by writing

const a = ["zero", "one", "two", "trhee"];

_.observe(a, () => {
  alert("something happened");
});

We call observe with array a and a callback that runs when a changes.

Conclusion

To watch for array changes with JavaScript, we use the underscore-observe library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *