Today, there was a question raised about selecting specific elements within an array from a certain index to the end of the array. This got me thinking about how I could achieve this using the filter
method.
One suggestion was to use the slice
method, which made sense as it allows you to extract elements between two specified indexes. But how could we accomplish the same task using the filter
method?
For example:
let arr = ['bug', 'cat', 'dog', 'flea', 'bat', 'hat', 'rat'];
let newArr = arr.filter(element => element >= element.indexOf(3));
console.log(newArr);
Although my attempt at solving this problem didn't work, the concept behind it was to filter out all elements in the array that have an index of 3 or greater and store them in a new array.