Converting an Object into an Array

Arnie Serrano
2 min readJan 12, 2021

Sometimes the data that you are fetching from in API is not in a format in which you can use methods like .map or .filter. This happened to me when taking an assessment for a job I had applied for and left me a little perplexed.

After I fetched the API, stored it in a students state by using setState, and tried to iterate through them to display their information, I got a type error and that .map wasn’t a function.

So in order to figure out what was going on, using typeof on the data I fetched showed that I wasn’t storing an array, but instead an object that was an array called students.

I had looked into how to convert objects into arrays, but then I thought about how I could just save effort and store the data as an array instead of doing an extra step of converting it.

Instead of just storing data with this.setState(students: data) after fetching, I can store the students array by specifying the data with:

Now I can iterate through the elements for each student’s information and manipulate the data as needed.

--

--