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
0
votes
1 answer

Destructuring object passed like argument to a function

Don't know if is possible with ES6. I'm working in a project that have a function which is passed an Object with lots of properties. Right now the code is as follows: function foo({arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8}){ this.model = arg1; …
Barleby
  • 618
  • 2
  • 9
  • 20
0
votes
2 answers

How can I access my component state through destructuring?

I'm trying to access the this.state.timeRemaining value from within the componentWillMount() function. I've destructured the this.state object and renamed the value to "swag". I expect my console.log() statement to print out "5" (as I have set the…
Dkpalea
  • 13
  • 4
0
votes
1 answer

Object destructuring default using other destructured values

The follow works in node v8.11.4 and in babel transpiled JavaScript running on chrome const myFunc = ({ aryOfObjs, combinedObj = Object.assign({}, ...aryOfObjs), }) => console.log(combinedObj); myFunc({ aryOfObjs: [ { foo: 'bar'}, {…
rudolph9
  • 8,021
  • 9
  • 50
  • 80
0
votes
1 answer

Setting the values of previously initialized variables using object destructuring syntax

my situation is that I have a number of variables I want to set - however, their values are conditional on another variable. So here's what I mean var { columns, rows, tableColumnExtensions, sorting, …
M Xiao
  • 553
  • 1
  • 7
  • 14
0
votes
2 answers

JavaScript: Reverse object destructuring / refactoring code to update object

TLDR: How to use destructuring to speed up updating parts of one object based on another object of the same interface? I would like to use the new ECMA2015 - 2017 JavaScript to refactor my code. For simplicity let's say I have an object, looking…
0
votes
2 answers

How to default to another property when the first is null or undefined using ES6 Object destructuring

Looking for a neat way to access a second non-null property from an object when then first property is null or undefined using ES6 object destructuring. ES5 equivalent: var obj = { a: null, b: 2 }; var num = obj.a || obj.b; // num = 2 Using ES6…
0
votes
1 answer

return same object after destructuring in function params

Let's say I have an Object var bar = {hi: 1, there: 2}; I would like to, at the end of the function, return the same object that was passed while at the same time doing a destructuring assignment in the function param. It might look like this:…
anonrose
  • 1,271
  • 3
  • 12
  • 19
0
votes
1 answer

How to use ES6 multiple destructured arguments in function definition?

I want to know how to use destructured multiple arguments given to a function when both parameters are of same type. For example, consider I have the following code: var items = [ { name: 'Edward', value: 21 }, { name: 'Sharpe', value: 37 }, {…
Dane
  • 9,242
  • 5
  • 33
  • 56
0
votes
4 answers

Object destructuring for structuring a new object

i have an object coming from an API respone, looks like this: { // ... customerName: 'Jake', customerUserName: 'jak3', customerEmail: 'some@email.com', // ... } and i want to declare a new object named apiUser to use in my app which sould…
-1
votes
1 answer

Null guarding a destructure inside a map

Do you know is there a way of null guarding a destructure that happens inside a map? For example, if my array's first element's age is null then is there a way of writing it so that it doesn't crash when iterating over the destructured…
-1
votes
1 answer

Destructured objects and arrow functions

I'm taking an online course to learn react. A student posted a solution to a problem that I think is really elegant and I'd like to understand better. The code iterates through an array of items to create a list in the UI. function…
-1
votes
1 answer

How to assign value of array element by a arrow function

I need to set one of the array property using arrow function. In my sample code I have added random key and an arrow function. but when I console the output, I cant see the "random" key. Can this be done using arrow functions. let Allitems =…
-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…
-2
votes
1 answer

JavaScript: Getting a Uncaught ReferenceError: Cannot access 'title' before initialization

I am newbie to JavaScript. Here's the sample code. let options = { title: "Menu", width: 100, height: 200 }; console.log(options.title); let {title, width, height} = { options:title, options:width, options:height}; // clause 1 I am getting…
yapkm01
  • 3,590
  • 7
  • 37
  • 62
-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…
1 2 3
10
11