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").

Filter by
Sorted by
Tagged with
-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 ...
Péter Szilvási's user avatar
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 ...
user_51's user avatar
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 ...
GD1's user avatar
  • 99
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 ...
emreakdas's user avatar
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 ...
Akshat Sharma's user avatar
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....
msmith1114's user avatar
  • 2,771
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? ...
mishar's user avatar
  • 369
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 ...
CoJanks's user avatar
  • 109
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 ...
David Cleland's user avatar
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 ...
Ashish Gahlot's user avatar
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 ...
Suragch's user avatar
  • 486k
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 ...
Balkan's user avatar
  • 21
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,...
Gato's user avatar
  • 671
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 }; // ...
user31782's user avatar
  • 7,097
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 ...
Antonin L's user avatar
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 ...
Kathiravan S's user avatar
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 =...
isakgo_'s user avatar
  • 740
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 ...
user31782's user avatar
  • 7,097
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', ...
Xaree Lee's user avatar
  • 3,198
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 ...
Savior-code's user avatar
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 ...
Kostya GL's user avatar
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 ...
web-innov-design's user avatar
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&...
NNQ's user avatar
  • 1
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". ...
Emma's user avatar
  • 316
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) => { ...
AllThingsShiny's user avatar
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: "...
DanilTi's user avatar
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 ...
Blob's user avatar
  • 1
-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 ...
Géry Ogam's user avatar
  • 6,428
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: ...
TAREK KOTB's user avatar
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 ...
Spunc's user avatar
  • 338
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 ...
davypough's user avatar
  • 1,847
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 ...
Barış Can Ateş's user avatar
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===...
Jordan's user avatar
  • 19
-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?...
jetxs's user avatar
  • 5
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 ...
agnul's user avatar
  • 12.7k
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 ...
Ramsey's user avatar
  • 3
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 ...
Abdullah Naseer's user avatar
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', ...
Wahidullah Shadab's user avatar
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 ...
lucype9297's user avatar
-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 ...
chamberlainpi's user avatar
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 =...
scheebler's user avatar
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 = { ...
edd's user avatar
  • 59
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 ...
botana_dev's user avatar
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 ...
brittle_spirit's user avatar
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 ...
rwacarter's user avatar
  • 1,925
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}}}...
Mikhail-Khoroshev's user avatar
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 ...
Zaid Malek's user avatar
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} =...
A.Lee's user avatar
  • 69
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 "...
TheTargetAudience's user avatar
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 ...
oneneozero oneneozero's user avatar

1
2 3 4 5
27