I have an array of objects within a useState hook, it looks something like this:
const[list,setList] = useState(null)
function x() {
setList([
{id:1,name:a},
{id:2,name:b},
{id:3,name:c},
]
}
I want to create a function that will delete a certain amount of these objects from the begining of the array. For example if I were to run the function delete(2), it would delete the first two objects in the array. Leaving list equal to only [{id:3,name:c}]
3
Answers
My Answer:
It would be help. you can use
array.filter()
function easily.If you only wish to remove elements from the beginning of the array you could use the
shift()
function. Theshift()
removes only the first element of an array. Using this function you could build something like this:You can then take the result of this function and set it in your list.
To use this function, you can call it with the desired number of items to delete:
This will remove the first two items from the list.