I am working on a project and am encountering an obstacle on something rather small. I am trying to take an array that has a string and a nested array. Remove the string assigning it to a new variable and then destructure the nested array into two new variables. I seem to be having problems as there is an undefined placeholder and I can't seem to find a way to get around it. I think I must be messing something up simple, but I would appreciate it if you could help me understand my mistake. Any help would be great.
Here is the code which I can't seem to get working.
let operandArray = ['x', [1, 1]]
let temporaryArray = [];
const operator = operandArray.shift();
temporaryArray = operandArray[0];
console.log(temporaryArray);
let [operand1, operand2] = [...operandArray];
console.log(operandArray);
console.log(operand1, operand2)
.as-console-wrapper { max-height: 100% !important; }
CodePudding user response:
You can simply use this.
const operandArray = ['x', [1, 1]]
const [str, [x, y]] = operandArray;
console.log(str, x, y);
You may wanna have a look at this blog post for a more detailed explanation of the desctructuring syntax.