本文主要介绍js数组差集(js数组交集),下面一起看看js数组差集(js数组交集)相关资讯。
介绍数组的交、并、差集给大家分享,如下:由于es5的方法,下一版会兼容,对应的polyfill要先加数组。原型。=数组的索引。原型。||| function (searchelement,location) {var index = 1的索引;从指示器=位置* 1 | | 0;for (var k = 0,length = this.length;k amplt;长度;k){ if(k gt;= location } = = this { k search element){ index = k;;打破;} }回报指数;};array . prototype . filter = array . prototype . filter | | function(fn,context){ var arr = { };;if(type of fn = function){ for(var k = 0,length = this.length;k amplt;长度;k ){ fn.call (context,this {k},k,this arr . push(k)this { });}}报酬;};依赖数组权重移除方法:数组权重array . prototype . unique = function{ var,{,};for(var i = 0;;我这.长度;我){如果(!{ this { } }){ { this { } } = true;r . push(ben { i });}}返回r;相交元素由属于集合a和集合b的元素组成..array.intersect = function (arr1,arr 2){ if(object . prototype . tostring . call(front)= { object array }对象。原型。托斯特林。调用{ object(arr 2)= array }){ return arr 1 . filter(function(v){ return = 1 })} use array . intersect({ 3 },{ 3,4,5,6 });//{,}集合a和集合b的并集元素到所有构成元素的组合。array.union = function (arr1,arr 2){ if(object . prototype . tostring . call(front)= { object array } object . prototype . tostring . call { object(arr 2)= array }){返回arr1.concat(arr2)的唯一组合。}}使用array.union({ 3},{1,3,4,5,6 });//{1 2 3 4 5 6}差集差集:属于一个集合但不属于b集合的元素。b的一组差:元素属于集合b,不属于集合。array . prototype . minus = function(arr){ if(object。原型。托斯特林。call(arr)= { object array }){ reactive interarr = array . intersect(this,arr);//交叉数组返回this . filter(function(v){ returns inter。(v) = 1的索引)}使用var arr = {1,2,3,4 };arr . minus({ 2 });//{1,3}以上是本文的全部内容。希望能帮到你,也希望大家多多支持。
了解更多js数组差集(js数组交集)相关内容请关注本站点。