-
Notifications
You must be signed in to change notification settings - Fork 85
Ignoring properties #37
Comments
Not sure this is relevant; just adding to the discussion. Note that in the array case, this is valid syntax: let x = [1, , 3] Whereas for objects, this isn't: let x = {a: 1, b: , c: 3} Still, I like your proposal; seems better than using a variable name such as |
On another note, what about spreading? let o = {a: 1, b: 2}
let oCloneMinusB = {...o, b: } Should this be supported? |
Using a legal variable name such as I'd be happy with: let x = {a: 1, b: 2, c: 3, md5: '83b0f2d70d7546ee634f801264c4a9b3'};
let { md5: null, ...y } = x;
let omit = 'md5';
let { [omit]: null, ...z } = x; But if that was supported, I'd expect to be able to do something similar with arrays: let x = [1, 2, 3]
let [null, ...y ] = x; // → y = [2, 3] |
I definitely wouldn't like to see special casing let { void md5, a, b, ...z } = x
// Or
let { !md5, a, b, ...z } = x The easiest way for now would just be to use a function: function skip(obj, props) {
const result = { ...obj };
for (const prop in props) {
delete result[prop]
}
return result
}
let { a, b, ...z } = skip(x, ['md5'])
// And if either the bind or pipeline operator is added it could be improved
// to something more readable:
let { a, b, ...z } = x::skip('md5', ...othersToSkip)
let { a, b, ...z } = x |> skip('md5', ...othersToSkip) |
@nunocastromartins I'd like to respond to a couple of things in your comments: let x = [1, , 3];
console.log(Object.keys(x)); // ["0", "2"] There would be no benefit to supporting this syntax in object literals: let x = {a: 1, b: , c: 3}
console.log(Object.keys(x)); // ["a", "c"] - so why mention 'b' at all? To address your second comment: no I don't think that syntax should be supported; mainly because there's nothing comparable to support spreading arrays while excluding specific indexes. Note: your example would naturally be written as: let o = {a: 1, b: 2}
// let oCloneMinusB = {...o, b: } // not supported
let { b:, ...oCloneMinusB } = o; // do this instead |
There's a little bit of discussion in issue #16 about filtering or ignoring properties.
The problem with this is the introduction of the unwanted variable
md5
. This can be avoided by using:This approach also works with computed property names:
However, this code is at risk of a
Cannot destructure undefined
error if the property value isnull
orundefined
.We need another way.
Array destructing allows this:
Perhaps this would the closest comparable syntax:
Does this proposal require anything other than making the AssignmentElement optional in the AssignmentProperty production?
The text was updated successfully, but these errors were encountered: