"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
-1
votes
1 answer
destructured object not accesible in of statement
My code looks like this
it's aiming to observe a modal content when it's open
const data = {
modal: false,
button: null
};
const updateData = () => {
const {modal, button} = data
if (document.querySelector('.modal')) {
modal =…
-1
votes
1 answer
Get values of nested JSON with unknown names Javascript
I have a deeply nested JSON object with some arbitary value as the name. I can not know the value ahead. A simplified example below. I can't seem to think of a way to get the value I need
{
"bigJSON":{
"5":{
"3":[
{
…

Ola John Ajiboye
- 123
- 1
- 1
- 9
-1
votes
2 answers
How can I get last element of an array by using Destructuring
I am learning ES6 Destructuring
I am getting an error Rest element must be last element
Here is what I have right now
const siblings = ['John','Jack','Amanda','Allie','Bob']
let [firstSiblingName,...middleAllSiblingsName,lastSiblingName] =…
user11910224
-1
votes
1 answer
Can I destructure nested variables in one line and still cope with undefined values?
I have a variable and I want to destructure it
const myFunc = ({name}) => {
console.log(name)
}
myFunc({name: 'hey'})
But what if the variable is not there?
const myFunc = ({name}) => {
console.log(name)
}
myFunc({name2: 'hey'})
It…

GWorking
- 4,011
- 10
- 49
- 90
-1
votes
3 answers
Object destructuring into class properties not working
The instance of this class is created like this:
this.example = new Example(this.prop1, this.prop2);
Then, I'm trying to destructure these properties like this:
export default class Example {
constructor(params) {
const { prop1, prop2 } =…

rpivovar
- 3,150
- 13
- 41
- 79
-1
votes
1 answer
Constructor Destructuring with exposed parameters
I have a constructor which uses destrucuring to simplify what needs to be passed to create the object with the proper defaults.
export class PageConfig {
constructor({
isSliding = false
}: {
isSliding?: boolean;
getList: (pagingInfo:…

johnny 5
- 19,893
- 50
- 121
- 195
-1
votes
2 answers
Destructing object as function args
const obj = {
foo: 1,
bar: 2
};
let fn = (...obj) => {
console.log(foo, bar); // ReferenceError: foo is not defined
};
fn(obj);
I basically want to use the object props directly inside the function without having to…

eozzy
- 66,048
- 104
- 272
- 428
-1
votes
3 answers
Destructuring nested params, undefined error
I keep getting payload undefined when destructuring:
let videosArray = [];
if (payload.videos) {
const {
payload: { videos }
} = action;
videosArray = videos;
}
return videosArray;
How can I check for undefined? I have tried the check…

Bomber
- 10,195
- 24
- 90
- 167
-1
votes
1 answer
How this de-sructuring syntax of array updating state in react.js
I was going through a react.js code where I encountered de-structuring array syntax really confusing.
I commented on author's post, but didn't got any response.
case Actions.AC_UPDATE_TASK:
item = action.payload.item;
props =…

Deepankar Singh
- 193
- 1
- 13
-1
votes
3 answers
Function parameter destructuring
Is there a way I can destructure my data in my function arguments?
const AgendaItem = ({ item }) => {
const { venue, organiser, startTime, endTime, description } = item;
return (
Location: {venue}
…

Bomber
- 10,195
- 24
- 90
- 167
-1
votes
1 answer
JavaScript destructuring in reduce() method not working with Internet Explorer 11?
I use the JavaScript reduce() method to count how many entries have the same values.
If I use the code with the Firefox 52 browser everything works fine. But if I use the code in Internet Explorer 11, the debugger told me, that there is an error in…

InFlames82
- 493
- 6
- 17
-1
votes
1 answer
Nested destructuring
Say I have an object with a shape like so:
{
rows: [
{some: fields,
go: here}]
}
, and say that, in a particular case, I knew that the length of rows is 1. How could I extract {some: fields, go: here} through destructuring?
I have…

Abraham P
- 15,029
- 13
- 58
- 126
-1
votes
2 answers
How to destructure a JSON object containing two objects into an array?
I have a JSON object, color, that consists of two JSON objects. I need to transfer these two JSON objects to an array. How can I do that?
i.e. {blue, purple} to [blue, purple]

Morgan4568771
- 153
- 2
- 4
- 14
-2
votes
0 answers
I am getting an "undefined" result while trying to destructure an array
Getting wrong results when destructuring array into independent variables
I tried to switch the values of two variables using this notation "[Agered,Kr]=[Kr,Agered]" and it didnt work,they didnt change. Later in the code, I am trying to destructure…

ManuelPrhyme
- 1
- 2
-2
votes
2 answers
Is there a way to identify if a function's return value is being destructured or used as is?
Let's say I'm using a method in this way:
const {a, b} = foobar()
// or...
const obj = foobar()
const {a, b} = obj
Is there a way from within the foobar function to tell whether it's result is being destructured immediately, or stored in a…

chamberlainpi
- 4,854
- 8
- 32
- 63