JSON.parse()), there is no such functionality built-in for CSV files, so in this article I will try to implement said functionality in ES6.
Convert an array to CSV
The above code is pretty straightforward. It uses
Array.join() to combine each subarray into a string, separating the values based on the
delimiter provided, then uses
Array.join() a second time to create a multi-line string, our CSV output, with all the array’s data.
Convert CSV to an array
Serializing an array is easy, but say you have a CSV file and want to convert it back to an array. In that case, you need a deserializer that reverses the above process:
The procedure here is essentially the same as before, but backwards. The file is separated into lines using
String.split(), then converted into individual values using
String.split() a second time based on the
delimiter provided. A finishing touch is to allow the user to decide if the want to omit the first row, as sometimes CSV files use the first row to store column names.
Convert CSV to an array of objects
Based on the last remark from the above snippet, we can easily figure out a way to convert CSV files to an array of objects, using the first row as the property names:
This is starting to get more complex now. The first thing we have to do is obviously grab the property names list from the first row, similarly to what we did before, using
Array.slice(). Then comes the object creation part, which is done using
Array.map() as we did before. The only major difference is that we use
Convert an array of objects to CSV
Here comes the trickiest part — serializing an array of objects. Why is this tricky and complicated? Because sometimes you have objects with a different number of properties or whose properties are in a different order. And that creates a few problems when trying to serialize into a uniform structure like CSV.
There are plenty of ways to remedy this, such as making certain assumptions about structure, ensuring that objects have the same number of properties etc., but the most versatile function I could write requires the developer to specify which properties need to be serialized:
It might not look like much, but there’s quite a lot going on here. First off, we need to create an array of lines for the CSV string and set the first row equal to the list of properties we want to serialize from the array of objects. Then, we use
Array.reduce() to create each row, with properties in the correct order and empty values for non-existent properties. Finally, we use the spread operator (
Array.join() to output a multi-line CSV string.
Considerations and improvements