Reputation: 68720
So this works:
var object = { 'a': 1, 'b': '2', 'c': 3 };
// Underscore/Lodash
var result = _.omit(object, ['a', 'c']);
console.log(result)
// output: { 'b': '2' }
// Native
var { a, c, ...result2 } = object;
console.log(result2)
// output: { 'b': '2' }
... but it doesn't work if my key has dashes:
var object = { 'my-key': 1, 'b': '2', 'my-secret': 3 };
// Underscore/Lodash
var result = _.omit(object, ['a', 'c']);
console.log(result)
// output: { 'b': '2' }
// Native
var { 'my-key', 'my-secret', ...result2 } = object;
console.log(result2)
// Error: SyntaxError: missing : after property id",
// expected output: { 'b': '2' }
Upvotes: 3
Views: 51
Reputation: 92461
When you use destructing like this you are assigning values to names even if you aren't using them. my-key
isn't a valid variable name, so can't restructure into that name. You can, however, rename the keys in the destructuring and avoid the problem:
var object = { 'my-key': 1,'b': '2','my-secret': 3};
var {'my-key': k, 'my-secret': k2, ...result2} = object;
console.log(result2)
Upvotes: 5