Discard a variable in a JavaScript object spread by assigning it to _
I had some code with spread syntax where some variables were being captured and used, and others were being saved into a catchall props
variable.
There was a variable I wanted to discard – not capture and use, and not add to props
. I could just capture it and not use it, but it might be flagged as an unused variable.
Here’s a simple example, where I capture the name
variable and discard lastRoll
:
const shape = {
name: 'd20',
sides: 20,
color: 'red',
lastRoll: 7,
};
const { name, lastRoll: _, ...diceProps } = shape;
console.log(name);
// 'd20'
console.log(diceProps);
// { sides: 20, color: "red" }