I have a state like so:
const [array, setArray] = useState([])
I know I can use below code to add an item to an array:
setArray(oldArray => [...oldArray, item])
How can I remove the first element of this array?
const names = ['Luke', 'Eva', 'Phil'];
var [first, ...rest] = names;
console.log(rest); // ['Eva','Phil']
source
You can also use Array.prototype.slice():
const arr = ['item 1', 'item 2', 'item 3', 'item 4']
console.log(arr.slice(1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With