Table of Contents
Using toString()
method
To write array to CSV in JavaScript:
- Declare an array with some elements in it.
- Use the
toString()
method. This method will convert the array into the CSV(Comma-Separated Value). - Finally, console the value to see the result.
Follow the below program:
1 2 3 4 5 6 |
const myArray = ['Cricket', 'ICC', 'Football', 'FIFA'] const arrayToCsv = myArray.toString() console.log(typeof(arrayToCsv)) |
Output:
1 2 3 |
Cricket,ICC,Football,FIFA |
Using join()
method
To write array to CSV in JavaScript:
- Declare an array that consists of some elements
- Use the
join()
method and pass the symbol as an argument that will be used in joining the elements. This method returns the result in the string form. - Finally, console it to see the result.
Follow the below program:
1 2 3 4 5 6 |
const myArray = ['Cricket', 'ICC', 'Football', 'FIFA'] const arrayToCsv = myArray.join(',') console.log(arrayToCsv) |
Output:
1 2 3 |
Cricket,ICC,Football,FIFA |
We used comma(,)
to join the array elements. In your case, you can use other symbols also.
Our purpose is to write CSV(Comma-Separated Value) from an Array in JavaScript.
Using valueOf()
method
To write array to CSV in JavaScript:
- The
valueOf()
method works exactly as thetoString()
method. You can follow the steps that are followed while using thetoString()
method. All you need to do is to use thevalueOf()
instead oftoString()
.
Follow the below program:
1 2 3 4 5 6 |
const myArray = ['Cricket', 'ICC', 'Football', 'FIFA'] const arrayToCsv = myArray.valueOf() console.log(arrayToCsv) |
Output:
1 2 3 |
Cricket,ICC,Football,FIFA |
Write Array of Objects to CSV
To write Array of Objects to CSV:
- Declare an array that consists of some objects.
- Define the column headers and then use the
spread(…)
operator as well as themap()
function to separate the key-value pairs from the array. - Finally, use another
map()
function andjoin()
method to separate the values with acomma(,)
Console.log()
them to see the output.
Follow the below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const arrayOfObject = [ { stdId: 1, stdName: "Rahul" }, { stdId: 2, stdName: "John" }, { stdId: 3, stdName: "Virat" } ] const arrayOfObjectToCSV = [ [ "Student ID", "Student Name" ], ...arrayOfObject.map(data => [ data.stdId, data.stdName ]) ] .map(el => el.join(",")) .join("\n") console.log(arrayOfObject) console.log(typeof(arrayOfObject)) console.log(arrayOfObjectToCSV) console.log(typeof(arrayOfObjectToCSV)) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[ { stdId: 1, stdName: 'Rahul' }, { stdId: 2, stdName: 'John' }, { stdId: 3, stdName: 'Virat' } ] I am --> object Student ID,Student Name 1,Rahul 2,John 3,Virat I am --> string |
We used the spread(...)
operator to extract the key-value pair and used the map()
function to convert the Object data into the array.
Now, we get the element into an array form. We used the mapping function and the join()
method in this array. This will join the array elements with a comma(,)
and convert them into the string.
Another join()
method is used to separate the strings into a new line.
Finally, We consoled arrayOfObject
as well as used the typeOf()
method. It shows that it is an Object
.
We consoled arrayOfObjectToCSV
and also used the typeOf()
method here. It shows String
.
That’s all about how to write array to CSV in JavaScript.