How to get array subset in JavaScript

1 Answer

0 votes
function getSubset(array, startIndex, length) {
    // Extract a subset of the array
    return array.slice(startIndex, startIndex + length);
}

const arr = [3, 7, 9, 0, 4, 2, 1, 8];
const startIndex = 2; // Start index for the subset
const len = 4;     // Number of elements in the subset

const subset = getSubset(arr, startIndex, len);

console.log("Subset: " + subset.join(", "));


   
/*
run:
    
Subset: 9, 0, 4, 2
       
*/

 



answered Mar 24 by avibootz
edited Mar 24 by avibootz
...