I have an array of items
const test = [
{
id: '1',
},
{
id: 'a',
},
{
id: '3',
},
{
id: 'b',
},
{
id: '5',
},
{
id: 'c',
},
{
id: '7',
},
];
These objects are always going to be in the same order. I’d like to be able to remove all items in order from the start until a specific id
is reached.
I thought I could do this by just looping through until one is found and then pushing the items coming after that to a separate array:
let skippedArray = []
let skipUntilIndex = undefined
test.forEach((item, i) => {
if(item.id === 'b') {
skipUntilIndex = i
}
if(skipUntilIndex && i >= skipUntilIndex) {
skippedArray.push(item)
}
})
// [
// {
// id: 'b',
// },
// {
// id: '5',
// },
// {
// id: 'c',
// },
// {
// id: '7',
// },
// ]
console.log(skippedArray)
But this seems pretty dirty. Is there a better way to achieve this? I don’t mind using lodash or similar libraries.