You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+91-5Lines changed: 91 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,15 +104,101 @@ The second consequence is that in a particular object spread, a minus operator c
104
104
105
105
For sure, there are existing alternatives to the proposed minus operator.
106
106
107
-
First of all, it's the [delete operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete). Although, it cannot be used declaratively, meaning that the object produced by object spread must be assgned to a variable first, and only then we can remove the property from it by its key.
107
+
1. The [delete operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete). Although, it cannot be used declaratively, meaning that the object produced by object spread must be assgned to a variable first, and only then we can remove the property from it by its key:
108
+
```js
109
+
const obj = {
110
+
a: 'aValue',
111
+
b: 'bValue',
112
+
key: 'value',
113
+
};
108
114
109
-
Second approach is using `Object.keys(obj).reduce(...)`, which is a block of repeated code that takes a bit of time to read and understand.
115
+
// To avoid mutating the original object, we create a copy
116
+
const result = { ...obj };
117
+
// Then we imperatively delete the key
118
+
delete result.key;
119
+
// And since the above line isn't an rvalue, we have to return the result explicitly
120
+
return result;
121
+
```
110
122
111
-
The third approach is even more imperative – it's `for ... of` loop or any related alternative.
123
+
2.`Object.keys(obj).filter(...).reduce(...)` which is a block of repeated code that takes a bit of time to read and understand:
124
+
```js
125
+
constobj= {
126
+
a:'aValue',
127
+
b:'bValue',
128
+
key:'value',
129
+
};
112
130
113
-
The fourth possible solution is to manually assign all the properties, manually omitting the ones that has to be omitted. It's quite a lot boilerplate code.
131
+
// A bit wordy, ineffective, and too difficult to read
And the last possible solution is to assign undefined to the key to be removed – but it's far from the ideal way to solve that issue, because the key would still exist in the object, meaning that if we enumerate through all the properties, there will be an undefined value for that key.
138
+
3. The third approach is even more imperative – it's `for ... of` loop or any related alternative.
139
+
```js
140
+
constobj= {
141
+
a:'aValue',
142
+
b:'bValue',
143
+
key:'value',
144
+
};
145
+
146
+
// A bit wordy `for ... of` loop
147
+
constresult= {};
148
+
for (const [key, value] ofObject.entries(obj)) {
149
+
if (key !=='key') {
150
+
result[key] = value;
151
+
}
152
+
}
153
+
// Also, loops aren't rvalues and thus, we have to return explicitly
154
+
return result;
155
+
```
156
+
157
+
4. Another possible solution is to manually assign all the properties, manually omitting the ones that has to be omitted. It's quite a lot boilerplate code.
158
+
```js
159
+
constobj= {
160
+
a:'aValue',
161
+
b:'bValue',
162
+
key:'value',
163
+
};
164
+
165
+
// `obj` might have too many properties here and this might be inconvenient
166
+
// to manually enumerate all of them
167
+
return {
168
+
a:obj.a,
169
+
b:obj.b,
170
+
};
171
+
```
172
+
173
+
5. Using spread with destructuring operator, to filter out all the undesired properties (for example, [`const { propertyIDontWant, ...newObject } = origObject; return newObject;`](https://github.com/devlato/proposal-plus-minus-spread/issues/1)).
174
+
```js
175
+
constobj= {
176
+
a:'aValue',
177
+
b:'bValue',
178
+
key:'value',
179
+
};
180
+
181
+
// `result` can't be returned directly from here, and also, an unnecessary variable `key` is created
182
+
const { key, ...result } = obj;
183
+
// So we have to return explicitly
184
+
return result;
185
+
```
186
+
187
+
6. And the last possible solution is to assign undefined to the key to be removed – but it's far from the ideal way to solve that issue, because the key would still exist in the object, meaning that if we enumerate through all the properties, there will be an undefined value for that key.
188
+
```js
189
+
constobj= {
190
+
a:'aValue',
191
+
b:'bValue',
192
+
key:'value',
193
+
};
194
+
195
+
return {
196
+
...obj,
197
+
// Even though `obj[key]` is `undefined`, `obj.hasOwnProperty('key')` would return true,
0 commit comments