Merging many objects
Object.assign()
combined with the rest and spread operators can shallow-merge an arbitrary number of objects.
// because of the two `...`, the function accepts any number of objects
const merge = (...objects) => Object.assign({}, ...objects)
// we can merge two objects into a single one
merge({ id: 5 }, { title: 'This is nice', description: 'Very nice' })
// ⇒ {
// id: 5,
// title: 'This is nice',
// description: 'Very nice'
// }
// we can pass as many objects to the function as we want; in case of
// duplicate keys, the last value always wins
merge({ name: 'Joe' }, { age: 27, email: 'joe@swanson.com' }, { age: 43 })
// ⇒ {
// name: 'Joe',
// email: 'joe@swanson.com',
// age: 43
// }
More fire tips
Removing the largest number from an array
In JavaScript, we can remove the largest value from an array of numbers in two steps: we first need to find that number, then we can filter it from the array.
Read fire tip