Current location - Training Enrollment Network - Mathematics courses - Several methods of finding array difference set with JS
Several methods of finding array difference set with JS
The first one: Foreach does not need to consider the compatibility of IE8. This method can find the difference set of arr 1 minus arr2.

arr 1? =? ;

arr2? =? ;

var? Subset? =? Function? (arr 1,? arr2)? {

var? set2? =? New? set(arr 2);

var? Subset? =? ;

$.arrayIntersect? =? Function (a,? b)

{

Return? $. Merge ($. grep(a,? Function (1)

{

Return? $.inArray (me,? b)? ==? - 1;

})? ,? $.grep(b,? Function (1)

{

Return? $.inArray (me,? a)? ==? - 1;

})

);

};

Windows. Console? & amp& amp? console.log(? $.arrayIntersect(alpha,beta)? );

//The result is equal to 4. The third set method using ES6 is similar to the foreach method.

var? Subset? =? Function (arr 1,? arr2)? {

var? set 1? =? New? set(arr 1);

var? set2? =? New? set(arr 2);

var? Subset? =? [];

For what? (let? Articles? Yes? set 1)? {

What if? (! set2.has(item))? {

subset . push(item);

}

}

Return? Subset;

};