Using Array’s filter method can solve your problem. The specific implementation is very simple, and others have also answered it.
Solution with the help of third-party libraries
If you don’t mind referencing third-party libraries, it is recommended that you introduce lodash. This library contains a large number of methods for processing arrays. If you have many array operation scenarios, it is highly recommended.
It has a function specifically to deal with this problem, called difference. Of course, a classmate said before that you can also use without, but it is not as convenient to use as difference.
The "_" in the code below is a default object after introducing lodash. All methods defined by lodash are under it, a bit like the "$" used after introducing jQuery
var a = [1,2,3,4,5,6];
var b = [2,3,6];
var result = _.difference(a, b); // result=[1,4,5]
https://lodash.com/docs/4.17....
Use native solution
Using Array’s
filter
method can solve your problem. The specific implementation is very simple, and others have also answered it.Solution with the help of third-party libraries
If you don’t mind referencing third-party libraries, it is recommended that you introduce lodash. This library contains a large number of methods for processing arrays. If you have many array operation scenarios, it is highly recommended.
It has a function specifically to deal with this problem, called difference. Of course, a classmate said before that you can also use without, but it is not as convenient to use as difference.
The "_" in the code below is a default object after introducing lodash. All methods defined by lodash are under it, a bit like the "$" used after introducing jQuery
Why bother using the
loadash
,直接用数组的filter
method: