简体   繁体   中英

Get the N elements from array based on the position

I want a function that returns the sub array which takes a position & the no. of elements I want. I think there may be some algorithm to find the pivot point or something & from that I can get the sub array, but I totally forgot it.

Example: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I want 6 elements
if position = 0, then I want [1, 2, 3, 4, 5, 6]
if position = 1, then [1, 2, 3, 4, 5, 6]
if position = 2, then [1, 2, 3, 4, 5, 6]
if position = 3, then [1, 2, 3, 4, 5, 6]
if position = 4, then [2, 3, 4, 5, 6, 7]
if position = 5, then [3, 4, 5, 6, 7, 8]
if position = 6, then [4, 5, 6, 7, 8, 9]
if position = 7, then [5, 6, 7, 8, 9, 10]
if position = 8, then [5, 6, 7, 8, 9, 10]
if position = 9, then [5, 6, 7, 8, 9, 10]
simply get the middle of N elements based on the position I pass.

I can write up my own loop which will contain multiple if-else conditions to get it done. But I feel there may be some easy way to do it.

I didnt include my incomplete code snippet because I strongly feel there must be some algorithm to do this.

What you want is : Array.prototype.slice(...)

It's neatly documented here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

var n = 6;
var start = Math.max(0, Math.min(Math.floor(position-n/2), a.length-n));
return a.slice(start, start+n);

Simple way:

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

function getSubArray(idx, _length, _array) {
  return _array.slice(idx, idx + _length);
}

var subArray = getSubArray(3, 6, a);

You could use an offset for the postion and get the the start value first for slicing.

 var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n = 6, i, start; for (i = 1; i < 12; i++) { start = Math.max(Math.min(i - n / 2, a.length - n), 0); console.log(i, ': ', a.slice(start, start + n).join()); } 

Your only need is to check if you are not gonna check a pos that doesn't exist. Like :

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var n = 6; // Number of result you want
var x = 8; // Pos you want

// If you gonna exceed your length, we got only the n last element
if((x+(n/2)) > a.length) { 
    console.log(a.slice(a.length-n)); 
// Otherwise, if under 0, we got the n first
} else 
    if((x-(n/2)) < 0) { console.log(a.slice(0,n) ); 
// Default case
    } else { 
console.log(a.slice((x-(n/2)),(x+(n/2))));
}

This is not the smartest way, but he can give you some hint. I used the slice as other mentionned to avoid a lot of if, but you should do GENERIC test.

Something like this :

a = [1,2,3,4,5,6,7,8,9,10];
n = 6;
function split(position) {
    var start = Math.min(Math.max(position - Math.floor(n/2), 0), a.length - n);
    var stop = Math.min(start+n, a.length);
    return a.slice(start, stop);
}

No need for the Math object at all. You may simply do as follows;

 function getArr(a,n,d){ n = n - 4 < 0 ? 0 : a.length - d > n - 4 ? n - 3 : a.length - d; return a.slice(n,n + d); } var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], diff = 6; for (var i = 0; i < 10; i ++) console.log(JSON.stringify(getArr(arr,i,diff))); 

no need of if-else you can use arr[position] to arr[8]. have you got

function getArr(arr,position,requiredNumbers){
return arr.slice(position, position+requiredNumbers);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM