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
1
vote
5 answers

Destructure object properties from properties provided in an array

I need to make a function getPropertiesData(list) that takes a list of properties and an object containing those properties and only return properties of the object that match in the list. Illustration: function getPropertiesData(['x', 'y']){ …
moctarjallo
  • 1,479
  • 1
  • 16
  • 33
1
vote
1 answer

Destructure arbitrary object property

Is there a way I can destructure object into its arbitrary property value and the rest of the properties? So far, my attempts didn't give much success: const x = {a:1, b:2, c:3}; const obj2pieces = (obj,propName) => { ({propName,...rest} =…
1
vote
1 answer

React Native: Why does object destructuring not work inside regular functions?

Using use object destructuring to destructe my state before sending a call to my REST API and it only works inside of an arrow function for me. I tried calling it inside of a regular function and I kept getting an error that it was undefined. Code…
bzlight
  • 1,066
  • 4
  • 17
  • 43
1
vote
2 answers

Correct way of object destructuring

I have a scenario, where I receive an obj from a promise, and need to add some keys of this object to another object. For example: // Received from promise object_1 = { name: 'SH' }; // Want to add object_1.name to object_2 object_2 = { …
shwz
  • 426
  • 1
  • 6
  • 22
1
vote
2 answers

Why aren't these destructuring assignments equivalent?

I'm trying to nest these destructuring assignments such that context1 and context2 are initialized to market[pair.context] and market[pair.target] respectively: // set market to this[pair.market] or empty object const { [pair.market]: market =…
neaumusic
  • 10,027
  • 9
  • 55
  • 83
1
vote
1 answer

ES6 default parameters with destructured object as second parameter referencing first parameter

I am writing a method that takes two parameters: the first a jQuery object that contains a form, the second an "options" object with default parameters, some of which reference the form in the first parameter. I know that a default parameter can…
1
vote
0 answers

How to use destructuring assignment with let

I would like to use let together with ES6 syntax, such as: let results let error; try { { results } = getResults(); } catch (err) { error = err; } return results; Is it possible?
zavr
  • 2,049
  • 2
  • 18
  • 28
0
votes
0 answers

Accessing data from heavily nested dynamic json object

I have this response coming from Profit and Loss Report of Quickbooks, how to deal with this data effectively for calculations, can someone help me out? Sample JSON response ->…
0
votes
1 answer

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? In other words, I'm wondering if syntax like: let…
0
votes
1 answer

Destructure an object that has the possibility of being undefined as a one-liner in a parameter list

Given an interface MyType: interface MyType { options?: { field0: string, field1: string, field2: string }; } Mapping over an array, myArray, which has type MyType[], one can destructure the options attribute like this: myArray.map(({ options })…
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
0
votes
0 answers

ES6 object destructing

In ES6, is ({width: this.width} = this.canvas); the same as this.width = this.canvas.width;? If not, what will the correct code be?
0
votes
1 answer

Destructure object, remove array when passing to component

I would like to pass the paging data to my component without passing the docs array. "docs": [ ], "totalDocs": 18, "offset": 0, "limit": 10, "totalPages": 2, "page": 1, "pagingCounter": 1, "hasPrevPage": false, "hasNextPage": true, "prevPage":…
Bomber
  • 10,195
  • 24
  • 90
  • 167
0
votes
1 answer

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: false, skills: ["Python", "Django"] }, { title: "Sayed", age: 33, available: true,…
0
votes
2 answers

Typescript Parameter Object Destructuring

I am getting a Typescript error for props.className below: type PropsType = { handleChange?: Function; props?: { [key: string]: any }; }; export default function Search({ handleChange, ...props }: PropsType) { return (
0
votes
3 answers

How to destructure array inside object in js?

How to destructure array inside object in js? let data = { names: ["Sam", "Tom", "Ray", "Bob"], ages: [20, 24, 22, 26], }; let /* some code */ = data; console.log(name2); // "Tom" console.log(age2); // 24 console.log(name4); //…
SIkaro
  • 11
  • 3