"Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable" ("Common Lisp the Language").
Questions tagged [destructuring]
1338 questions
-2
votes
1 answer
How do I do "Reverse destructuring"
We all know about destructuring objects in ES6+
const myObject = {name: 'John', age: 30, eyeColor: 'brown'}
const {name, age, eyeColor} = myObject
... is there a "cleaner" way to do the following?
const name = 'John'
const age = 30
const eyeColor =…

anthony galligani
- 386
- 1
- 3
- 16
-2
votes
1 answer
Create array with checked boxes with javascript
i want to create a combination game checkin some elements with checkboxes
i need to create a array while the user check a checkbox and compare with the correct array
i dont know who do that
this is my inputs
…

Santiago Diaz
- 5
- 1
- 3
-2
votes
2 answers
Shortcut for destructuring items from multiple indexes in Javascript
Given an array of less than 160 items, I want to extract three fields and make a copy of them;
const item1 = {name:'Item-1'};
const item2 = {name:'Item-1'};
const item1 = {name:'Item-1'};
...........
const item_N_minus_one =…

Niteesh Bhargava
- 157
- 1
- 10
-2
votes
1 answer
Cannot access 'y' before initialization javascript
I am trying to swap values of two variables using array destructuring, however I get the error Cannot access 'y' before initialization. Can someone please explain why it is not working and provide a solution
my code :
let x = 'bob'
let y =…

Kligan Braganca
- 3
- 2
-2
votes
1 answer
ES6 destructuring with dynamic key
In case, we need to use dynamic key to parse data out of some nested object and destructure it as below, is it required to be assigned to some alias as below. I am getting compilation error otherwise.
const { [dynamicKey] : isAliasVarReqd } =…

copenndthagen
- 49,230
- 102
- 290
- 442
-2
votes
1 answer
Variable declaration error and confusion in JavaScript object destructuring
I have a restaurant object where there are nested objects
const restaurant = {
name1: 'Classico Italiano',
location1: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu:…

Linus
- 39
- 4
-2
votes
1 answer
Destructuring a json object in Javascript
How would access the _id and state values?
Here's the data
{
"data": {
"totalSamplesTested": "578841",
"totalConfirmedCases": 61307,
"totalActiveCases": 3627,
"discharged": 56557,
"death": 1123,
…

Tobi2612
- 1
- 1
-2
votes
1 answer
Javascript - destructuring array of objects
trying to get better grasp on destructuring.
If I have object like this:
const starWars = {
count: 2,
data: [
{name: 'Luke Skywalker', films: ['Empire Strikes Back', 'The Force Awakens']},
{name: 'Han Solo', films: ['New Hope', 'Return of…

Tom N
- 51
- 9
-2
votes
1 answer
Destructuring simple nested Object with ES6/React
I am having this object
const launch = {
"flight_number": 102,
"mission_name": "GPS SV05",
"mission_id": []
"rocket": {
"rocket_id": "falcon9",
"rocket_name": "Falcon 9",
},
"details": null,
…

Sean Saoirse
- 91
- 5
- 17
-2
votes
4 answers
Need help in this array destructuring and intialize value in object
let Arr = ["a","b","'c','d'","e","f"]
need to store value in object like with fixed key name
obj = {hey: "a", hello: "b", how: "'c','d'",are: "e",you:"f"}

Raghav Sehgal
- 25
- 5
-2
votes
2 answers
Multiple destructuring assignments return undefined
TLDR Edit: I was confusing object destructuring w/ arrays and spread syntax. Below edit does the trick to recursively unpack the nested object.
let nextLevel = Object.values(obj)
return goingDeep(...nextLevel)
Following is the original question,…

Jeffrey Boyer
- 1
- 1
- 1
-2
votes
2 answers
Angular 7: destructuring with Typescript interface, getting errors
I have an interface like so:
And a component like so:
But I am getting this error:
How can I destructure my values so that Typescript will be happy?

Phillip Berger
- 2,317
- 1
- 11
- 30
-2
votes
1 answer
What is the proper way to get an element from an array with array destructuring?
Pretty much everything is in the title but i'll give you an example :
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not…

Anatole Lucet
- 1,603
- 3
- 24
- 43
-2
votes
2 answers
Javascript Parameter format
I don't understand the difference between these two functions:
function animal({ cat }){
return cat
}
function animal(cat){
return cat
}
why use one over the other? Is there an advantage?

olafsadventures
- 151
- 1
- 10
-2
votes
1 answer
What does putting curly brackets around a variale when initializing it does exactly?
Example :
const {http} = require('http');
I have seen something about "destructing" and have read some resources about that, but I am still very confused about what it actually does.

Balocre
- 175
- 2
- 10