[英]Finding Elements in an Array that are larger than those in another Array
There are 2 arrays data1
and data2
. 有2个数组
data1
和data2
。 The desired result is an array containing all the elements in data1
which are larger than the largest element in data2
[+ the 3 before as defined in variable buffer
]. 期望的结果是一个数组,其中包含
data1
中的所有元素,这些元素大于data2
的最大元素[+变量buffer
定义的3之前的元素]。 In other words, the result should also include the previous 3 elements in data1
preceding the matched elements. 换句话说,结果还应包括
data1
中匹配元素之前的前3个元素。 Both arrays are already sorted in ascending order. 这两个数组已经按升序排序。
My code below does what is required, but is there a better way to do the same thing? 我的下面代码执行了所需的操作,但是有没有更好的方法来执行相同的操作? Will regex be faster?
正则表达式会更快吗?
Data set 资料集
data1 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
data2 = [102, 104, 106, 108]
buffer = 3
The desired result is [106, 107, 108, 109, 110]
期望的结果是
[106, 107, 108, 109, 110]
Algorithm 算法
largestData2 = _.max(data2)
data1LargerThanData2 = _.filter(data1, function(data1) {
return data1 > largestData2
})
smallestData1LargerThanData2 = _.min(data1LargerThanData2)
sliceStart = data1.indexOf(smallestData1LargerThanData2) - buffer
result = data1.slice(sliceStart)
var data1 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110],
data2 = [102, 104, 106, 108];
var maxData2 = Math.max.apply(Math, data2);
var newData = [];
data1.map(function(val) {
if(val > maxData2) {
newData.push(val);
}
});
var index = data1.indexOf(newData[0]); //getting index of 1st element
var prev3Arr = [data1[index-3], data1[index-2], data1[index-1]]; //forming prev 3 elements
var finalResult = $.merge( prev3Arr, newData); // merging both arrays
console.log(finalResult);
Check your console for result: [106, 107, 108, 109, 110]
检查控制台以获取结果:
[106, 107, 108, 109, 110]
Start by getting the largest value in data2
: 首先获取
data2
的最大值:
var max = Math.max.apply(null, data2);
Then get all of the numbers from data1
that are greater than max
: 然后从
data1
中获取所有大于max
:
var arr = data1.filter(function (el) {
return el > max;
});
Use value of the first value in arr
to find our starting point in data1
and take the appropriate chunk of elements from it. 使用
arr
中的第一个值的值在data1
找到我们的起点,并从中获取适当的元素块。
var index = data1.indexOf(arr[0]) - 3;
var chunk = data1.slice(index, index + 3);
Merge arr
into chunk
. 将
arr
合并为chunk
。
chunk.push.apply(chunk, arr); // chunk = [106, 107, 108, 109, 110]
Assuming the arrays are always in order: 假设数组总是有序的:
var result;
var largestData2 = data2.slice( -1 );
_.each( data1, function( x, index ){
if( x > largestData2 ){
result = data1.slice( index - buffer - 1 );
return false;
}
} );
If the two arrays are sorted ascending, you can omit max
and just pick the last element 如果两个数组升序排列,则可以省略
max
而仅选择最后一个元素
var largestData2 = data2[data2.length - 1];
To find the position in data1
being larger than largest_data2
, you can use a binary search and then go back three elements 要查找
data1
中大于largest_data2
的位置,可以使用二进制搜索,然后返回三个元素
var smallestData1LargerThanData2Index = binarySearch(data1, largestData2);
var sliceStart = smallestData1LargerThanData2Index - buffer;
if (sliceStart < 0)
sliceStart = 0;
var result = data1.slice(sliceStart);
Update: 更新:
Updated JSFiddle to actually use underscore.js . 更新了JSFiddle以实际使用underscore.js 。
You want all the values from data1 that are larger than the largest value of data2? 您是否希望data1中的所有值都大于data2的最大值? If so How might I find the largest number contained in a JavaScript array?
如果是这样, 如何找到JavaScript数组中包含的最大数字? this might help
这可能会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.