So basically, can the unused part of the destructured object be condensed into a variable?
This obviously won't work because "toss" is already declared,
THIS IS JUST TO ILLUSTRATE THE QUESTION
const obj = { a:1, b:2, c:3, d:4 }
const toss = {b,c};
const {...toss, ...keep} = obj;
console.log(keep); // { a:1, d:4 }
CodePudding user response:
If toss
is meant to be a list of keys to ignore - then you can define it as an array of strings - rather than the invalid way you defined it that would throw an error
Then it's a simple one liner (there's probably other ways to do this, as there always is in javascript, this is the first that came to mind though)
const obj = { a: 1, b: 2, c: 3, d: 4 }
const toss = ['b', 'c'];
const keep = Object.fromEntries(Object.entries(obj).filter(([k]) => !toss.includes(k)))
console.log(keep);
however, even simpler, since your code is all hard coded anyway
const obj = { a: 1, b: 2, c: 3, d: 4 }
const {b, c, ...keep} = obj;
console.log(keep);
CodePudding user response:
Try this.
const _0 = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
const _1 = {'a': 1, 'b': 2, 'c': 4};
const _2 = function(_0, _1) {
let _2 = Object.keys(_0);
let _3 = _2.length;
let _4 = '';
let _5 = {};
let _6 = 0;
while (_6 !== _3) {
_4 = _2[_6 ];
if (typeof _1[_4] === 'undefined' || _0[_4] !== _1[_4]) {
_5[_4] = _0[_4];
}
}
return _5;
};
const _3 = _2(_0, _1);
console.log(_3);
This allows destructuring with {'key': value}
pair comparisons instead of an array of keys.
A while()
loop with Object.keys()
is also faster and more browser-compatible than Object.entries()
.