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?
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']
You can also use Array.prototype.slice():
const arr = ['item 1', 'item 2', 'item 3', 'item 4']
console.log(arr.slice(1))