Questions tagged [object-destructuring]

Use this tag for JavaScripts object destructuring syntax.

JavaScript object destructuring allows one to replace code like:

const object = {foo: 'bar', baz: 'bing'};
const foo = object.foo;
const baz = object.baz;

With this destructuring assignment syntax:

const object = {foo: 'bar', baz: 'bing'};
const {foo, baz} = object;
155 questions
2
votes
3 answers

Combining objects with object destructuring and the turnary operator

I'm trying to detemine the style based on a few boolean variables (sticky and isHidden). I know that I can combine 2 objects by using {...object1, ...object2} I am trying to apply the same logic to a nested object, so that style would be the result…
Sam
  • 1,765
  • 11
  • 82
  • 176
2
votes
3 answers

prints two values without destructuring

i'm trying to learn javascript destructuring and got stuck, did not find similar to this so im asking if somebody could enlighten me. I have two kind of question 1: here i have destructure in argument ( is it destructure?) and above i have function…
user10727653
2
votes
2 answers

ES6 function does not have access to `this` while de-structuring the Object

I was trying out the below question in ES6 learning material const circle = { radius: 10, color: 'orange', getArea: function() { return Math.PI * this.radius * this.radius; }, getCircumference: function() { return 2 * Math.PI *…
2
votes
5 answers

Javascript: Is there a cleaner way to extract object properties?

I have the following JS code: let obj1 = { prop1: 1, prop2: 2, prop3: 3, prop4: 4 } let obj2 = { prop1: obj1.prop1, prop2: obj1.prop2, } Here I create obj2 which has certain select properties of obj1. Is there a…
gkeenley
  • 6,088
  • 8
  • 54
  • 129
2
votes
2 answers

Object destructuring with Short-Circuit Evaluation

Is it possible to do achieve something like this? const obj1 = { name: 'tom' } const obj2 = { age: 20 } let { name, age } = obj1 || obj2 Getting as a result -> name = 'tom' and age=20 The code above doesn't work, as it evaluates the condition one…
2
votes
0 answers

Delete Multiple Object Properties Using Destructuring

I am trying to delete multiple computed properties from an object at once using destructuring. Something like this const a = {b: 1, c: 2 , d: 3}; const forbiddenKeys = [ "b", "c"]; // pretend this is computed const { ...forbiddenKeys, ...rest } =…
2
votes
2 answers

Nested object destructuring with computed object property names - react state

I'm trying to setState but I can't figure out how I can destructure the rest of object which has a dynamic property name. In this example id is a dynamic value for each input in my forms. After computing the state it looks like this: { …
Maciej Czarnota
  • 559
  • 2
  • 8
  • 16
1
vote
3 answers

In JavaScript, how can I conditionally assign value to object with destructuring and short circuit evaluation?

Let's say I have this set up: const objA = { name: "Jacob", email: "jacob@email.com" }; const objB = { lastName: "Smith" }; Why can I do this: const lastName = objA.lastName || objB.lastName; But not this? const { lastName } = objA || objB; Was…
1
vote
1 answer

How does dependent object destructuring assignment work in JavaScript?

This is apparently a valid destructuring assignment despite qux depending on bar: const { foo, bar, qux = () => bar } = myObject; How does this work since the documentation…
1
vote
0 answers

How to set default dependent props when using destructuring assignment?

Destructuring assignment is a well known method for assigning default props to React components: interface ButtonProps { onClick: () => void; disabled?: boolean; getTextColor?: (theme: Theme) => string; } const Button = ({ onClick, …
1
vote
2 answers

Destructuring an object with nested array

Basically I want to destruct this object to get this result but in the console I see u is not defined The object: const game = releases: { "Oath In Felghana": ["USA", "Japan"], };` My code: const { releases: { "Oath In…
zakramy
  • 19
  • 2
1
vote
1 answer

destructuring with spread operator and typing

I have this object: const ABCD = { a: 1, b: 2, c: 3, d: 4 } I can destructure it, collect the rest of it using the "spread" operator, and type the variables like this: const {a, b, ...restOfIt}: {a: number, b: number} = ABCD; But how do I…
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
1
vote
3 answers

How to destructure object properties with string key name

How to destructure this object: const game = { title: "YS", developer: "Falcom", releases: { "Oath In Felghana": ["USA", "Japan"], "Ark Of Napishtim": { US: "20 USD", JAP: "10 USD", }, Origin:…
B-M Amine
  • 39
  • 1
  • 6
1
vote
1 answer

How do you access react hooks in multiple components? What am I doing wrong with my react hook?

Im trying to pass a state value into a component. Why is it working in one component and not working in another component in the same folder? I have the hooks in here. Im trying to access "currentGuess". In this function I initialize the state of…
1
vote
1 answer

How to store the contents of the request body correctly in Express middleware of POST request

I have a middleware in my React Express project that stores a new user that is created as follows: const uuid = require('uuid/v4') const HttpError = require('../models/http-error') let DUMMY_USERS = [ { id: 'u1', name:…
1 2
3
10 11