"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
Array destructuring in prototype function parameters
Is there a way in which I can destructure an array in the parameters of its prototype functions?
For example, I can use an Array.prototype function such as forEach to evaluate the value of each array element and log an individual sub-array value…

Brandon McConnell
- 5,776
- 1
- 20
- 36
-1
votes
1 answer
destructuring - turn an array into an object by grabbing the 1st element
How can I use destructuring to create a new object that deep copies this:
{
"data": [
{
"type": "b",
"id": "2",
"other": {
"name": "hello"
}
}
]
}
and spits out one of these:
{
"data": {
"type":…

Mike
- 609
- 12
- 36
-1
votes
2 answers
How can I add array values to an object using destructuring?
I have variables here firstName which is Faisal and lastName which is Iraqi.
let str = "Faisal Iraqi";
let [firstName, lastName] = str.split(" ");
console.log(firstName, lastName);
console.log(str.split(" "));
so I should add this properties to my…

qLeima-
- 3
- 2
-1
votes
1 answer
Can I destructure an array in a Javascript arrow function?
Does any version of Javascript support destructuring of arrays in arrow functions? E.g.
const items = [ [1, 2], [3, 4] ];
const sums = items.map( [a, b] => a + b );

Josh Hansen
- 917
- 1
- 9
- 20
-1
votes
1 answer
Destructing assignment javascript doesnt work
I've tried to destruct a json. This is the original:
const shoppingLists = [
{
id: 0,
name: "empty shopppinglist",
location: "",
targetDate: "",
priority: "",
isFinished: false,
items: [{
name: ""
}]
},
{
id: 1,
name: "full…

Collin
- 339
- 1
- 3
- 13
-1
votes
1 answer
javascript-es6: unable to destructure an array of objects. UNEXPECTED TOKEN ERROR
I have an array of objects and I am trying to destructure it so that I can directly use the property names in react-table's column's accessor property.
This is the raw JSON which is actually being fetched via an API call.
Not sure, where I am going…

Nauman
- 894
- 4
- 14
- 45
-1
votes
1 answer
Destructuring the nested objects fetched from an API call using "curly braces method"
I fetched the data like this:
constructor(){
super();
this.state = {
fetchedData : []
}
}
componentDidMount(){
fetch("https://api.randomuser.me")
.then(response => response.json())
.then(data => {…

rebel
- 3
- 2
-1
votes
3 answers
ES6 javascript destructuring large objects in react
I suspect the answer is "you can't" but there are a lot of people here who are brighter than I.
In reviewing (and re-writing) code I wrote a year-ish ago (before I learned redux), I have a react component that contains the following destructing of a…

Charles Goodwin
- 584
- 5
- 8
-1
votes
1 answer
Javascript Error : While using array destructuring
var [a,b] = list
console.log("a ",a) // a = 1
console.log("b ",b) // b = 2
[a,b] = [b,a]
console.log("a ",a) // a = 2
console.log("b ",b) // b = 1
VM247:3 a 1
VM247:4 b 2
VM247:6 Uncaught TypeError: Cannot set property…

Arvindh Ashok
- 13
- 3
-1
votes
2 answers
Can't make an output with a forEach function in a template string
const categoriesLght = document.querySelectorAll('#categories .item').length;
console.log(`There are ${categoriesLght} clasifications.`);
const h2s = document.querySelectorAll('h2');
const heading = h2s.forEach(element =>
…

ElMuchacho
- 300
- 1
- 12
-1
votes
3 answers
Selectively Destructure an object, based on key name
Let's say I have this object:
const someProps = { darkMode: true, underlineMistakes: false, spellingView: (...), grammerView: (...) };
I do not necessarily know the names of any of the props, except that 1+ end in 'View'.
and I want to only…

AncientSwordRage
- 7,086
- 19
- 90
- 173
-1
votes
2 answers
How to understand destructuring syntax in following codes
I found some codes when reading a article. And i can't understand it. But when i tested it, it worked! can anyone told me what is parameter means? it is a destructuring?
function test ({ a = '1', b = '2', c = '3' } ={}) {
console.log(a, b,…

FerntreeGuy
- 53
- 5
-1
votes
1 answer
Use/advantage of Array Destructing?
I have recently moved to 'airbnb-base' eslint style-guide. There i am get an error when i try to reference the array elements with index eslint(prefer-destructuring).
eg
let a = {};
// get an error saying eslint(prefer-destructuring)
a.b =…

AnandShiva
- 906
- 11
- 22
-1
votes
1 answer
JavaScript destructuring Objects
I have the following object (data model):
const TimeSheetSchema = mongoose.Schema({
category: {
type: String,
require: true
},
duration: {
start: Date,
end: Date
},
isDeleted: {
type:…

Marcel
- 9
- 2
-1
votes
1 answer
Destructure res.headers
I'm trying to destructure the response.headers of my axios.get request because I only need the x-csrf-token. It is always the second position. This is the how the res.headers response looks
{
'content-type': 'application/json; charset=utf-8',
…

JangoCG
- 823
- 10
- 23