DEV Community

Cover image for I used the SPREAD OPERATOR for the first time 🤯🤯
Ayoazeez26
Ayoazeez26

Posted on

I used the SPREAD OPERATOR for the first time 🤯🤯

What is a spread operator?

The spread operator is used when all elements of an iterable variable such as an object or an array needs to be included in some sort of a list. This has a lot of use cases, below is a basic description of what using the spread operator looks like:

Alt Text

I was recently consuming API endpoints and I came across an endpoint whose response is an array of arrays and each array item contains several objects which I needed to get. First instance was to do a forEach() to loop through each arrays. Then I proceeded to do another forEach() to get to the individual objects of each of the arrays. All was going fine until I decided to render those items to the DOM and it turns out only the last array of objects was getting rendered. This was a point of confusion because everything seems to be working fine. After a lot of trying to figure things out, I decided to use a spread operator after the first forEach loop to push each item in each of the arrays to an empty array. This makes it that all objects are in a single array
spread in action

Top comments (0)