Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having issues trying to destructure array to new variables for assignment

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; }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading