如何从数组中获取下一个N元素?

时间:2012-11-27 09:31:54

标签: javascript

需要一个从给定数组返回N个元素的函数,给定偏移量,但当偏移量大于数组长度时,它必须返回数组开头的元素。

接口
slice2(array, chunk, offset);

示例:

var array = [1,2,3,4,5];
slice2(array,2,2)输出:[3,4]
slice2(array,2,4)输出:[5,1]
slice2(array,3,4)输出:[5,1,2]

6 个答案:

答案 0 :(得分:7)

function slice2(array, chunk, offset) {
    var subarray = [];
    for (var i = 0; i<chunk; i++) {
        var ind = (offset + i) % array.length;
        subarray.push(array[ind]);
    }

    return subarray;
}

答案 1 :(得分:0)

内置的.slice()方法可能足以解决您的问题:

var slice = array.slice(1,3);

我设置了一个快速的jsfiddle,界面需要改变,因为它只允许出现

答案 2 :(得分:0)

对不起自己也不能帮忙:)。

以下函数可以处理更多场景,并且仅使用2个切片执行此操作。

// standard
slice2( [1,2,3,4,5], 3, 3 ); // [1, 4, 5]
// specify chunk as a negative number
slice2( [1,2,3,4,5], -3, 3 ); // [1, 2, 3]
// what if chunk is larger than array
slice2( [1,2,3,4,5], -7, 3 ); // [1, 2, 3, 4, 5]
// what if chunk is 0
slice2( [1,2,3,4,5], 0, 3 ); // []


function slice2( array, chunk, offset ) {

    var start1, len = array.length, end1, start2, end2, result;

    if( !chunk || !array ) return []; // if chunk or array resolve to falsy value.
    if( Math.abs(chunk) >= len ) return array;

    if( chunk < 0 ) {

        end1 = offset;
        leftover = offset + chunk;
        start1 = leftover < 0 ? 0 : leftover;
        start2 = leftover;
        end2 = len;

    } else {

        start1 = offset;
        leftover = ( offset + chunk ) - len;
        end1 = leftover > 0 ? ( offset + chunk ) : len;
        start2 = 0;
        end2 = leftover;
    }

    result = array.slice( start1, end1 );
    if( leftover ) result = array.slice( start2, end2  ).concat( result );

    return result;
}

小提琴here

答案 3 :(得分:0)

试试这个:

function slice2(array, chunk, offset){
    var end = offset + chunk,
        out = array.slice(offset, end);  // Get the chunk
    if(array.length < end){              // If the chunk should wrap
        out = out.concat(array.slice(0, end - array.length)); // Concatenate a the rest of the chunk, from the start of the array, to the output.
    }
    return out;
}

它不会手动循环遍历数组,只使用极少的计算。

答案 4 :(得分:0)

private static List sliceArray(List mainarray,int chunk,int offset){

    int size = mainarray.size();
    List<Integer> resultArray = new ArrayList<Integer>();

    if(!mainarray.contains(chunk)){
        return null;
    }

    int index = mainarray.indexOf(chunk);
    int doOffset = size - index;

    if(doOffset > offset){

        for(int element = 1 ; element <= offset; element++ ){
            resultArray.add(mainarray.get(index+1));
            index++;
        }

    }else if(doOffset == 0){
        for(int element = 0 ; element <= offset; element++ ){
            element = mainarray.get(element);
            resultArray.add(element);
        }

    }else{
        int position =0;
        for(int element = index ; element <= offset; element++ ){
            int value = 0;
            if(element < size-1){
                value = mainarray.get(element+1);
            }
            else
            {
                value = mainarray.get(position);
                position ++;
            }
            resultArray.add(value);
        }
    }

    return resultArray;
    // TODO Auto-generated method stub

}

答案 5 :(得分:0)

这是我所做的摘要。感谢@Andrei我刚刚修改了他的代码。因此,我想要的是一个函数,用于获取数组中的选定数字以及该数字之前和之后的x个数字。例如,[1,2,3,4,5,6,7,8]如果我选择2作为偏移量,将块选择为3,则输出将是:[8,1,2,2,3,4,5,6] 如果offset = 3,chunk = 3,op:[1,2,3,4,5,6,7]

function cyclicSlice(array, chunk, offset) {
var subarray = [];
for (var i = array.length; i>=(array.length-chunk); i--) {
    var ind = (offset + i) % array.length;
    subarray.push(array[ind]);
}
subarray = subarray.reverse();
const newOffset = offset + 1; // To avoid the repititon of the selected number
for (var i = 0; i<chunk; i++) {
    var ind = ( newOffset + i) % array.length;
    subarray.push(array[ind]);
}

return subarray;

}