ES6 Resting Spreading Objects Tricks
Here are 7 lesser-known tricks with ES6 Resting and JavaScript Objects that you can use to make your code more compact and clean.
1. Add properties to Objects.
You can clone an object and add properties at the same time. The following example clones the user object and adds a password property
const user = { id: 100, name: 'Howard Moon'}
const userWithPass = { ...user, password: 'Password!' }
user //=> { id: 100, name: 'Howard Moon' }
userWithPass //=> { id: 100, name: 'Howard Moon', password: 'Password!' }
2. Merge Objects
Merge 2 objects into 1 object
const part1 = { id: 100, name: 'Howard Moon' }
const part2 = { id: 100, password: 'Password!' }
const user1 = { ...part1, ...part2 }
//=> { id: 100, name: 'Howard Moon', password: 'Password!' }
Or you can merge with the following syntax:
const partial = { id: 100, name: 'Howard Moon' }
const user = { ...partial, ...{ id: 100, password: 'Password!' } }
user //=> { id: 100, name: 'Howard Moon', password: 'Password!' }
3. Remove (delete) a property (Exclude Properties)
You can remove a property by combining destructuring with the rest operator. The following example removes the password property
const noPassword = ({ password, ...rest }) => rest
const user = {
id: 100,
name: 'Howard Moon',
password: 'Password!'
}
noPassword(user) //=> { id: 100, name: 'Howard moon' }
4. Dynamically Exclude Properties
The following trick uses computed object property names to remove a property dynamically:
const user1 = {
id: 100,
name: 'Howard Moon',
password: 'Password!'
}
const removeProperty = prop => ({ [prop]: _, ...rest }) => rest
// ---- ------
// \ /
// dynamic destructuring
const removePassword = removeProperty('password')
const removeId = removeProperty('id')
removePassword(user1) //=> { id: 100, name: 'Howard Moon' }
removeId(user1) //=> { name: 'Howard Moon', password: 'Password!' }
5. Organize Properties
Move id to the first position
const user3 = {
password: 'Password!',
name: 'Naboo',
id: 300
}
const organize = object => ({ id: undefined, ...object })
// -------------
// /
// move id to the first property
organize(user3)
//=> { id: 300, password: 'Password!', name: 'Naboo' }
6. Default Properties
The following setDefaults function automatically adds the quotes: [] property if the object doesn’t have a quotes property.
const user2 = {
id: 200,
name: 'Vince Noir'
}
const user4 = {
id: 400,
name: 'Hello',
quotes: ["I've got a bad feeling about this..."]
}
const setDefaults = ({ quotes = [], ...object}) =>
({ ...object, quotes })
setDefaults(user2)
//=> { id: 200, name: 'Vince Noir', quotes: [] }
setDefaults(user4)
//=> {
//=> id: 400,
//=> name: 'Hello',
//=> quotes: ["I've got a bad feeling about this..."]
//=> }
7. Renaming Properties
Combining the techniques above, we write a rename function to rename a property like this:
const renamed = ({ ID, ...object }) => ({ id: ID, ...object })
const user = {
ID: 500,
name: "Hello World"
}
renamed(user) //=> { id: 5000, name: 'Hello World' }