我想在JavaScript中重叠数组。
var myArray = [[1, 5], [4, 9], [8, 12], [11,15], [18, 19]]
// my Output will be :* [[1, 15], [18, 19]]
因为1,15覆盖值之间的全部,并且18,19不是1,15。不是值重叠。
我尝试了下面的代码,但是它检查了所有数组,并且仅获得了一个1,19]输出。
function removeOverlap(arr1, arr2) { // Converting the arguments object to array: var argsArray = Array.prototype.slice.call(arguments); // Removing undefined: argsArray = argsArray.filter(function(el) { return el != undefined; }); // Alternative (not compatible with Internet Explorer): //argsArray = argsArray.filter(el => el); // We're looking for the min and max numbers, let's merge the arrays // e.g. transform [[1, 5], [3, 6], [2, 8]] into [1, 5, 3, 6, 2, 8] var merged = [].concat.apply([], argsArray); // Alternative, but it is not compatible with Internet Explorer: //var merged = Array.flat(argsArray); return [Math.min.apply(null, merged), Math.max.apply(null, merged)]; } // Sample: console.log(removeOverlap(myArrays[0], myArray[1], myArray[2], myArray[3], myArray[4]));
我想在JavaScript中重叠数组。 var myArray = [[1,5],[4,9],[8,12],[11,15],[18,19]] //我的输出将是:* [[1,15],[ 18,19]]因为1,15覆盖了值和18之间的全部,...
一种简单的解决方案是将范围内的每个数字添加到集合中,然后遍历该集合以确定连续数字: