Alternative to the spread operator.
TLDR: Object.assign(object, object)
I was doing some work on a serverless function and I didn't have ES6 support so I had to figure out how to supplement the spread operator. Below is an example of the spread operator with some objects that hold some food.
const moreFood = {'pizza': '🍕', 'steak': '🥩',}const food = { 'chocolate': '🍫', 'icecream': '🍦', 'pizza': 'pizza', ...moreFood }//results{'chocolate': '🍫', 'icecream': '🍦','pizza': '🍕','steak': '🥩',}
One of the alternatives to the spread operator is the Object.assign function. Here is the same function using the object.assign function.
const moreFood = {'pizza': '🍕', 'steak': '🥩',}const food = { 'chocolate': '🍫', 'icecream': '🍦', 'pizza': 'pizza' }Object.assign(food, moreFood)//results{'chocolate': '🍫', 'icecream': '🍦','pizza': '🍕','steak': '🥩',}
Side note:
If there is a duplicate key like in the example with pizza both the spread operator and the Object.assign function both will take what the right objects says pizza is.
Top comments(6)

- LocationOklahoma
- WorkWeb Developer at Life.Church
- Joined
If you just want the original value of pizza you just put the object to the right so inObject.assign(moreFood, food)
and with the spread operator you can{...moreFood, food}
For further actions, you may consider blocking this person and/orreporting abuse