There are 2 arrays data1
and 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
]. In other words, the result should also include the previous 3 elements in data1
preceding the matched elements. 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]
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]
Start by getting the largest value in data2
:
var max = Math.max.apply(null, data2);
Then get all of the numbers from data1
that are greater than 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.
var index = data1.indexOf(arr[0]) - 3;
var chunk = data1.slice(index, index + 3);
Merge arr
into chunk
.
chunk.push.apply(chunk, arr); // chunk = [106, 107, 108, 109, 110]
Demo .
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
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
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 .
You want all the values from data1 that are larger than the largest value of data2? If so How might I find the largest number contained in a JavaScript array? this might help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.