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
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
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
I think it;s dirty/unwanted question.
I have object name test, I just try to assign key and values(if value is valid).
In this below example x,y,z are variables, this variables are dynamic sometimes only we get value.
The below code is working fine, but I used everytime checked value is valid or not then i assign key and value into object.
Just I want to check some smart way to add key?
var test = {
a: "1",
b: "2"
}
var x = "3";
//here x value is dynamic, sometimes only we get value.
if (x) {
test.c = x;
}
var y = "4";
//here y value is dynamic, sometimes only we get value.
if (y) {
test.d = y;
}
var z = "5";
//here z value is dynamic, sometimes only we get value.
if (z) {
test.e = z;
}
console.log(JSON.stringify(test));
If, as in your code, the tests always check to see whether the value is truthy before adding to the object, you could use a Proxy:
const test = {
a: "1",
b: "2"
};
const testProx = new Proxy(test, {
set: (obj, prop, val) => {
if (val) obj[prop] = val;
}
});
testProx.c = 'foo';
testProx.d = null; // falsey, will fail the Proxy's test and will not be added to object
testProx.e = 'bar';
console.log(test);
If you need more complicated validating, such as different conditions for different keys, I'd suggest making an object indexed by key containing a function that returns whether the value for that key is valid:
const test = {
a: "1",
b: "2"
};
// just an example of having different conditions, this is not DRY code:
const testConditions = {
c: (v) => typeof v === 'string' && v[0] === 'c',
d: (v) => typeof v === 'string' && v[0] === 'd',
e: (v) => typeof v === 'string' && v[0] === 'e',
}
const testProx = new Proxy(test, {
set: (obj, prop, val) => {
if (testConditions[prop](val)) obj[prop] = val;
}
});
testProx.c = 'ccc';
// does not start with 'd', will fail the Proxy's test and will not be added to object:
testProx.d = 'fff';
testProx.e = 'eee';
console.log(test);
You can write it in a shorthand way like:
var x, y, z;
var test = {
a: "1",
b: "2",
c: x || null,
d: y || null,
e: z || null
}
console.log(JSON.stringify(test));
Bear in mind that x,y,z must be defined before your test variable or you'll get the error like Uncaught ReferenceError: x is not defined.
And also you can do more type checking with your x,y,z variables, with following syntax:
var x, y, z;
var test = {
a: "1",
b: "2",
c: (x == 3 ? x : null),
d: y ? y : null,
e: z ? z : null
}
console.log(JSON.stringify(test));
If it's coming from another variable then you can loop through that and check every key if the value is null and if not, add it to the test variable.
const main = {
x: 1,
y: 2,
z: 3
}
const test = {
a: 11,
b: 12,
c: 13
}
Object.keys(main).forEach((key) => {
if (main[key]) {
test[key] = main[key]
}
});
console.log(test);
Since the description of your question is not that much clear, I am answering assuming,
You need to validate every value you add into the object.
You are finally using the stringified object for further processing.
You need to automatically set the key for the values.
You may even use a proxy to do the same thing based on your requirement.
var test = {
currentKey:0,
add:function(val){
if(val){
this[String.fromCharCode('a'.charCodeAt()+(this.currentKey++))]=val;
}
},
toJSON:function(){
let obj={...this};
delete obj.currentKey;
delete obj.toJSON;
return obj;
}
}
let x = 90;
test.add(90);
let y = null;
test.add(y);
let z = 89;
test.add(z);
console.log(JSON.stringify(test));
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