This problem can be solved in a recursive manner.
First we need to declare two arrays, one which will contains the input (the given array) and the other will contains the result (initially empty)
var givenArray = [1, 2, 3];
var resultArray = [];
Now let's create our recursive function that will push a subset array to our result array:
function getSubArray(array, position) {
if(position === givenArray.length) {
resultArray.push(array);
return;
}
getSubArray(array.concat(givenArray[position]), position+1);
getSubArray(array, position+1);
}
Now to start set all the subsets to the resultArray
we just need to call our getSubArray
function with an empty array and a start position which is 0 as arguments
getSubArray([], 0);
After that just remove the last element of the resultArray
if you want to remove the empty set ([]) from the result
resultArray.splice(-1,1);
You can test the algorithm online via this link: https://jsbin.com/xigipihebe/edit?js,console