How to get array subset in Node.js

1 Answer

0 votes
function getSubset(array, startIndex, length) {
    // Extract a subset of the array
    return array.slice(startIndex, startIndex + length);
}
 
const arr = [9, 0, 5, 7, 3, 1, 8, 6];
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: 5, 7, 3, 1
        
*/

 



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