Questions tagged [destructuring]
"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").
1,342
questions
-1
votes
1
answer
55
views
Is destructuring Structs faster than multi-line variable assignment in Rust?
I have come across destructuring Structs or Enums during pattern matching. After sometime, It got used to it, and it became one of my favorite features in Rust. However, destructuring a struct's field ...
0
votes
1
answer
26
views
Is it possible to combine match statements with array destructuring in PHP to achieve something like pattern matching in functional programming?
I was hoping for something like this to work:
$bar = match (foo()) {
[] => throw Exception,
[, ] => throw Exception,
[$x] => $x,
};
Where, I'd test if foo() gives array of ...
0
votes
1
answer
21
views
React Native - Destructuring parameter item in renderItem FlatList attribute
I'm porting a ReactJS project over React Native, and I'm trying to learn Native's Components. I want to display a list/sequence of track cards after fetching their data from Musixmatch and Spotify.
In ...
1
vote
0
answers
37
views
Why can't internal destructuring be done using rest when object destructuring in JavaScript?
I cannot use it like this.
const { a, ...{ b } } = { a: 1, b: 2 };
I wonder what's the reason? I found this on mdn but I didn't fully understand it.
"On the other hand, object destructuring can ...
0
votes
0
answers
26
views
why do we use "[]" or square brackets while setting an `event.target.name` to as a key in useState Hook [duplicate]
here is the code notice how [event.target.name] is wrapped inside square brackets, i think this is destructuring but my question is event is an Object and objects are destructured with curly brackets ...
1
vote
2
answers
70
views
Playwright promise.all returning page/context?
So I saw a bit of confused and I was a bit confused what it was doing:
private async SwitchPage(): Promise<Page> {
const [newWindow] = await Promise.all([
await this.context....
0
votes
1
answer
44
views
JavaScript: Is a new array created by a rest element in destructuring assignment?
I'm wondering if array destructuring syntax that uses a rest element (be it an object or array) creates a new array in memory that is then used by that nested rest element's destructuring assignment? ...
1
vote
2
answers
34
views
How do I destructure and rename specific properties from a fetch response and save to a state object?
I am fetching data from the OMDB API and saving this data to a state. However, I am running into 2 questions/issues:
How can I destructure the data I receive while also renaming it and then saving ...
1
vote
3
answers
66
views
Can I have an indeterminate number of destructuring lists in a lisp macro lambda list?
I'm trying to write a macro that expands to an unspecified number of function calls, but I also want to be able to specify exactly one argument to be passed to each function in the macro call. I would ...
0
votes
1
answer
35
views
Conceptual understanding for Destructuring and State in React
Below code works fine. I just want to understand that after destructuring of name and value from e.target, why name needs to be wrapped in square brackets while setting data. Why can't we use variable ...
2
votes
3
answers
306
views
Dart nullable records with pattern matching: The matched value of type '(String, int)?' isn't assignable to the required type '(Object?, Object?)'
Given the following function:
(String, int)? getPair() {
return ('hello', 42);
}
I can access the multiple returns using the fields like so:
final pair = getPair();
if (pair != null) {
String ...
0
votes
2
answers
37
views
Difference between ts parameter definitions - myFunc({data}: {data: ExampleType}) {} vs myFunc(data:ExampleType) {}
I'm working on a TypeScript project and came across two different parameter definitions in some code. Both seem to achieve the same result, but I'm not sure if there's any significant difference ...
1
vote
1
answer
74
views
What is the difference between these two in Rust?
I'm learning Rust and came across the following:
let (x, y, z);
(x, y, z) = (1, 2, 3);
Is there any difference between that and this, besides the former being more compact?
let x;
let y;
let z;
(x, y,...
0
votes
1
answer
39
views
Why does the expression {x}.x throw error by itself, but within an expression it doesn't?
I have been reading about object initialization on mdn and came across the following example:
const a = "foo";
const b = 42;
const c = {};
// Shorthand property names
const o = { a, b, c };
// ...
0
votes
1
answer
44
views
Destructuring String.split() works in the console, but not in a file
I had a problem while destructuring the result of an String.split() operation.
Situation
This code is present in my index.html page in a <script> tag, but I tested it with a separate file, and ...
0
votes
0
answers
31
views
Array swap elements using Destructuring concept in JavaScript [duplicate]
I'm trying to learn swapping the elements in the array using destructuring concepts in javascript.
var arr = [1, 2, 3]
[arr[2], arr[1], arr[0]] = arr
console.log(arr);
I thought the console will ...
0
votes
1
answer
117
views
Why is it not object destructuring when calling useActionData() in react router dom?
When I call useActionData(), I don't get any object destruction. When I print to the console, I get name: 'isakGo'. The code looks like this
export async function action({request}) {
const formData =...
0
votes
1
answer
40
views
Why doesn't const { innerRef, ...props } = this.props; grab a refrence to this.props.innerRef in react ref forwarding?
After reading forwarding refs from react legacy docs and following the examples from the SO post on using ref forwarding on classes, I tried to create a working example as following:
class Card ...
2
votes
2
answers
52
views
Destructuring assignment for dict with default values in JavaScript
I saw a JS syntax as following:
export const ChartComponent = props => {
const {
data,
colors: {
backgroundColor = 'white',
lineColor = '#2962FF',
...
0
votes
2
answers
63
views
how do I pass in a object function into another using the arguments given
I am trying to pass in an 'order' function to 'orderDelivery' function through objects to synchronize with the order instead of hard coding it at the bottom
const restaurant = {
name: 'Classico ...
0
votes
1
answer
116
views
RTK Query: How can I get trigger from useLazyQuery conditionally?
How can I get trigger from useLazyQuery conditionally?
const [trigger] = props.useLazySearchQuery();
My goal is to get trigger only if useLazySearchQuery is received in props.
Of cource I can do ...
0
votes
1
answer
57
views
Can i pass a array to constructor of an class by destructuring in javascript
i want to create a class Statistics that'll take 25 inputs or maybe more or less varied number of inputs and does the calculation on it to find mean, median, mode, range, variance, standard deviation ...
0
votes
0
answers
34
views
Typescript Error when passing the rest prop {...others} to component of material-ui
I have created a UITooltip based on Tooltip component of material-ui. Here is my component:
import React, { FC } from "react"
import { Tooltip, TooltipProps } from "@material-ui/core&...
1
vote
2
answers
99
views
What is an example of "array destructuring" via "assignment pattern" in JavaScript?
This MDN documentation on destructuring assignment says that "for both object and array destructuring, there are two kinds of destructuring patterns: binding pattern and assignment pattern". ...
0
votes
2
answers
86
views
Destructuring in JavaScript during a John Smilga project
I am working on a John Smilga project, Comfy Store. I am attempting to destructure data from an API with the following code
let store = getStorageItem('store');
const setupStore = (products) => {
...
0
votes
1
answer
41
views
Destructurization of all object properties
Is there a possibility of destructurization assignment of an object without specifying all properties so that all properties become available without accessing the object.
let props = {one: "...
0
votes
0
answers
48
views
I'm using an array sort function to sort a nodelist but there is a lag when I try to append the array a parent node. Can I do this more efficiently?
See snippet of code below. I get the element nodes first and store them in a 'paragraph' variable. I then sort the array. Then I make clear the innerHTML of the parent element. I then loop through ...
-6
votes
1
answer
70
views
What does an object destructuring pattern used as the rest property of an array destructuring pattern stand for?
The MDN Web Docs have this example:
Using a binding pattern as the rest property
The rest property of array destructuring assignment can be another array or object binding pattern. This allows you to ...
0
votes
1
answer
110
views
Destructuring in switch statement gives 'variable is not initialized' error
let chosen = 3;
let myFriends = [
{ title: "Osama", age: 39, available: false, skills: ["HTML", "CSS"] },
{ title: "Ahmed", age: 25, available: ...
0
votes
2
answers
76
views
Handle destructoring in macro that creates defmethod methods
In my use case, I have a multimethod that takes two arguments, both of them maps. The first one is used, amongst other things, to compute the dispatch value. The second one is a kind of context ...
1
vote
2
answers
88
views
Speeding Up a Function Involving String Searches
I have one function that profiling shows consumes most of the run time. Is there perhaps a way to rethink this for a substantial speedup? (Running Common Lisp in SBCL)
(defun compatible-words (option1 ...
0
votes
2
answers
112
views
Destructuring nested map of maps - Clojure
I studying about destructuring in Clojure. I learned a bit and now I can able to destructure two-level nested maps. How can I destructure the deepest nested maps? I leaving here an example map that I ...
0
votes
2
answers
37
views
Destructuring error depending on order of variables
I encountered some strange destructuring behavior when reversing a linked list in this problem:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===...
-1
votes
1
answer
181
views
Is it bad practice to override the props in React functional components?
I'm trying to specify default values for optional props for a React component.
I can do this, and the I can get the value of the optional property 'y' correctly.
interface CompProps {
x: number;
y?...
0
votes
1
answer
58
views
Destructuring map function arguments in clojure: does the map need to go last?
I'm trying to define a function that takes a map and a regular argument, and I'd like to destructure parts of the map, something like
(defn do-stuff
[[{:keys [foo bar]} where] what]
(println foo ...
0
votes
1
answer
112
views
After destructuring assignment with 'let', can't I ressign a variable of them?
In practicing NestJS..
DTO is being used in Controller and I declared 3 variables for them by destructuring assignment.
At this time, I used 'let' keyword when declaring for a test.
export class ...
0
votes
1
answer
127
views
TypeError: education.map is not a function This error happened while generating the page. Any console logs will be displayed in the terminal window
Hi Everyone i am trying to do something that i have a main file of Nextjs name as Project.js and I am importing the components init, i am facing the issue in exporting the data from main file to ...
1
vote
1
answer
90
views
Destructure nested object with possible null values
I am receiving a nested object as a response from my API which looks like this:
myObject = {
name: 'client name',
address: {
building: 'building name',
block: 'block name',
...
0
votes
1
answer
20
views
Exporting uesState variable into another compenent/page
I'm a bit stuck. I've put the users input results into an array that can be used on another component. However when I try to destructure the 'newList' in another component/page it doesn't show ...
-2
votes
2
answers
67
views
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 ...
0
votes
1
answer
37
views
React - undefined properties of object after destructuring
I am trying to render a list of objects after a fetch, I get all the data, but at render time, properties are undefined. What am I doing wrong? Here is my code.
import React from 'react';
const Post =...
1
vote
1
answer
92
views
Spread operator and destructuring
interface Item {
id: string;
title: string,
}
interface ItemState {
entities: { [id: string]: Item };
}
const toBeDeleted = { id: '2', title: 'item_2' };
const state: ItemState = {
...
0
votes
1
answer
61
views
Redux reducers: items.map is not a function
This is a question with a learning purpose, I am stuck in a specific approach. I am playing a little bit with Redux to learn it and I have a question about a part of my code. Let's say that I have the ...
0
votes
2
answers
60
views
Destructuring primitive values in JavaScript
The doc page for destructuring assignment says
This means if you try to destruct a primitive value, the value will
get wrapped into the corresponding wrapper object and the property is
accessed on ...
0
votes
1
answer
58
views
Can a destructed assignment return any number of objects?
I have a series of Storybook stories, and a function that returns a ComponentStory object. I'm trying to think of a succinct way to clone a bunch of stories, without having something like this:
export ...
0
votes
1
answer
44
views
desctucrure object in JS {data: {user: {id, role}}} = useSession() throw an erorr
How can an object be destructured in a JS that can be initialized as undefined. For example, I need to destructure the session object that is returned by the useSession hook.
{data: {user: {id, role}}}...
5
votes
6
answers
480
views
Destructuring an array using spread operator on left side of assignment to collect all remaining elements in a single variable
I want to perform Destructuring in php just like in javascript code below:
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a,b,rest);
Output:
10 20 [ 30, 40, 50 ]
How can I preform that ...
0
votes
1
answer
59
views
JavaScript Destructuring Assignment [duplicate]
I am stuck here figuring out why the password argument from bcrypt.comapare(password, user.password) line keeps getting value from const {password, ...rest} = user; instead of const {email, password} =...
0
votes
1
answer
113
views
How can I change the value of a destructured object in React?
I'm learning react by following a guide, but it's outdated. I want to change the value of 'weight' that is inside the person object, but whenever I toggle the onChange I get an error that says "...
1
vote
2
answers
75
views
Rust Destructuring directly inside print without using any extra variables
I have an enum and a variable,
enum Message {
Write(String),
}
let msg = Message::Write("hi".to_string());
I want to print the String inside "msg". By destructuring directly ...