Deconstruct Object into Array of custom variable names [duplicate] - javascript

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2

You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

Related

Is it possible to provide a default value and destructure that value at the same time in Javascript?

Here's what I mean, I want to do something that is a combination of :
const {b: {c}} = a;
and
const {b={c:0}} = a;
I know I can break them apart like:
const a = {};
const {b={c:0}} = a;
const {c} = b;
console.log(c); //will get 0
But is there anyway to do them in a single statement? Something like (below code doesn't work):
const a = {};
const{b:{c}={c:0}} = a;
console.log(c); // Hoping to get 0
Assuming you want to get a default c value, you could use nested default values and destructuring.
const
getC = a => {
const { b: { c = 0 } = {} } = a;
return c;
};
console.log(getC({}));
console.log(getC({ b: {} }));
console.log(getC({ b: { c: 42 } }));

2 same variable module names from require [duplicate]

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2
You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

React/Javascript ES6 Object Destructuring Syntax [duplicate]

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2
You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

Destructuring different types in JS

For an object x
const x = {
a: 'foo',
b: () => { return 'bar' )
}
Is it possible to destructure x to get both a and b as strings in a single step?
{a, <something with b?>} = x
console.log(a, b) // 'foo bar'
On a broader scope, I am confused on how to cleanly destructure different types contained within a single object
In the real world, a is an invocation and b is a nested object
const x = {
a: c(), // returns an object
b: {
x: {...},
y: () => {...}
} // is an object
}
// is this even doable
{< some magic stuff >} = x
typeof a === typeof b //true
AFAIK, that's only possible with property getter if that is an option:
const x = {
a: 'foo',
get b() { return 'bar' }
}
const {a, b} = x
console.log(a, b) // 'foo bar'

ES6/ES2015 object destructuring and changing target variable

How can I rename the target during object destructing?
const b = 6;
const test = { a: 1, b: 2 };
const {a, b as c} = test; // <-- `as` does not seem to be valid in ES6/ES2015
// a === 1
// b === 6
// c === 2
You can assign new variable names, like shown in this MDN Example
var o = { p: 42, q: true };
// Assign new variable names
var { p: foo, q: bar } = o;
console.log(foo); // 42
console.log(bar); // true
So, in your case, the code will be like this
const b = 6;
const test = { a: 1, b: 2 };
let { a, b: c } = test;
console.log(a, b, c); // 1 6 2
Online Babel Demo

Categories