I have an array:
const savings = ["+10$", "-22.50$", "+5$", "+22.40$", "-3.5$"];
I want to show elements only in a certain range of array indexes. For example: how to show everything between array index 1 (-22.50$) and array index 3 (+22.50$)? All elements with lower or higher indexes should be removed.
There are many ways to do so:
savings.slice(1,4) // ['-22.50$', '+5$', '+22.40$']
savings.splice(1,3) // ['-22.50$', '+5$', '+22.40$']
...plus many other more complicated techniques including but not limited to:
in this case it's effectively just a .slice.
// ['-22.50$', '+5$', '+22.40$']
savings.filter((price, index) => index >= 1 && index <= 4)