There is an array of numbers [1,2,3,4,5,6,7,8,9,10]
I need to get all numbers from this sequence that are different from current for more than 2 items, but looped.
For example if current number is one, so new list should have everything except 9,10,1,2,3, or if current number is four so new list should be everything except 2,3,4,5,6.
Is there any technique how to make this, without creating multiple loops for items at start and at the end?
Thank you.
asked Jul 12, 2012 at 8:53
Marvin3
6,0518 gold badges39 silver badges50 bronze badges
-
Can you provide more examples?Florent– Florent2012年07月12日 08:59:25 +00:00Commented Jul 12, 2012 at 8:59
-
Added one more example, hope it's more clear now.Marvin3– Marvin32012年07月12日 09:20:53 +00:00Commented Jul 12, 2012 at 9:20
-
is the array always the same? If nto does it always loop on 10? does it always contain consecutive elements, etc. Should the order they are returned be the same as the original array or is order irrelevant?Chris– Chris2012年07月12日 09:23:00 +00:00Commented Jul 12, 2012 at 9:23
-
New order should be the same, but without those numbers. Length of array and number of items is dynamic (it can be array with 25 items, or with 300). And it's always consecutive.Marvin3– Marvin32012年07月12日 09:26:47 +00:00Commented Jul 12, 2012 at 9:26
3 Answers 3
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var exclude = function (start, distance, array) {
var result = [];
for (var i = 0; i < array.length; i++) {
var d = Math.min(
Math.abs(start - i - 1),
Math.abs(array.length + start - i - 1)
)
if (d > distance) {
result.push(array[i]);
}
}
return result;
}
answered Jul 12, 2012 at 10:17
Otto Allmendinger
28.5k7 gold badges71 silver badges79 bronze badges
I think this performs what you asked:
// Sorry about the name
function strangePick(value, array) {
var n = array.length
, i = array.indexOf(value);
if (i >= 0) {
// Picked number
var result = [value];
// Previous 2 numbers
result.unshift(array[(i + n - 1) % n]);
result.unshift(array[(i + n - 2) % n]);
// Next 2 numbers
result.push(array[(i + 1) % n]);
result.push(array[(i + 2) % n]);
return result;
} else {
return [];
}
}
Some tests:
var array = [1,2,3,4,5,6,7,8,9,10];
console.log(strangePick(1, array)); // [9,10,1,2,3]
console.log(strangePick(4, array)); // [2,3,4,5,6]
answered Jul 12, 2012 at 9:34
Florent
12.4k10 gold badges51 silver badges58 bronze badges
Comments
You may use javascript array.slice:
function get_offset_sequence(arr, index, offset) {
var result = [];
if (index - offset < 0) {
result = arr.slice(index - offset).concat(arr.slice(0, index + offset + 1));
}
else if (index + offset > arr.length - 1) {
result = arr.slice(index - offset).concat(arr.slice(0, Math.abs(arr.length - 1 - index - offset)));
}
else {
result = arr.slice(index - offset, index + offset + 1)
}
return result;
}
Example of use:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var index = 1;
var offset = 2;
for (var i=0; i < 10; i++) { console.log(i, arr[i], get_offset_sequence(arr, i, offset)) }
answered Jul 12, 2012 at 9:51
okuznetsov
2981 gold badge5 silver badges12 bronze badges
Comments
lang-js