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
4 answers

javascript assign variable to key-values extracted from object

how can i do something like the following const BigObject = { "id":"value", "img":"value", "name":"value", "otherData":"value", "otherData":"value", "otherData":"value", "otherData":"value", } var User = {id,img,name} = BigObject where User will…
Abd
  • 497
  • 4
  • 14
-2
votes
2 answers

JavaScript, destructuring vs assignation

I had this code: const availableDays = status.availableDays; And I had a suggestion to change with: const { availableDays } = status; They both are one line code, but I wonder if there is an explanation on why would be the destructuring a better…
pmiranda
  • 7,602
  • 14
  • 72
  • 155
-3
votes
2 answers

I am trying destructuring assignment in JavaScript but its giving undefined

const myObj = { name: "Rahul", age: 34, Roll: 23 } // console.log(myObj) const {x, y, z} = myObj; console.log(x) I tried to store values of myObj in these three variables using the destructuring assignment but its giving undefined.
usaka
  • 11
  • 3
-3
votes
2 answers

Difference between [...obj] and {...obj}

I know about the spread operator ... in JavaScript. We can use it on both arrays and objects: let user = { name: "aman", rollno: 11 } let newobj1 = {...user} let newobj2 = [...user] console.log(newobj1) console.log(newobj2) Why does newobj2…
-3
votes
1 answer

Why console.log(myObj) prints an error in JS.Bin?

Here is a simple exercise that i don't finish to understund: const myObj = { name: 'Max', age: 28 } const {name} = myObj; console.log(name); // prints 'Max' console.log(age); // prints undefined …
1 2 3
10
11