Write a program to convert an array of objects to a CSV string that contains only the columns specified using JavaScript
Last Updated :
23 Jul, 2025
When we want to store multiple values in one variable then instead of a variable, we use an array. which allows you to store multiple types of data for example you could have a string, an integer, etc.
In this article, we will learn to convert the array objects to CSV string data.
Given array objects ( key: value ) to CSV string comma-separated values and keys as header.
Input : [
Name: "geek",
Roll Number: 121,
Age: 56
]
Output: Name, Roll Number, Age
geek, 121, 56
Before getting into code we must know some about the following Array functions.
- Map() function: Array.prototype.map() function used when we want to transform each value of the array and want to get a new array out of it.
- Object.Key() Function: object.keys() method returns the key properties of this array. as we need a header for CSV data so our header is going to be the keys of an object so to get the header we use Object.key() method.
- Push() function: The Array.push() method is used to add one or multiple elements to an array. It returns the new length of the array formed.
- Join() function: The Array.prototype.join() method is used to join the values of an array into a string. The values of the string will be separated by a specified separator and its default value is a comma(, ).
Approach:
- Create an empty array first to store all data of an object in form of rows.
- Using the Object.keys() method fetches all keys of an object which are going to be the first row of the CSV table.
- map() method iterate over all objects and append all values to the "csvRow[]" array along with comma(,) separator using the join() method.
- push() method will push all data into "csvRow[]" array fetched by map() and Objects.keys().
- after mapping, each row new line will be added by the join("\n") method.
Example: Below is the implementation of the above approach:
JavaScript
<script>
const objectToCsv = function (data) {
const csvRows = [];
/* Get headers as every csv data format
has header (head means column name)
so objects key is nothing but column name
for csv data using Object.key() function.
We fetch key of object as column name for
csv */
const headers = Object.keys(data[0]);
/* Using push() method we push fetched
data into csvRows[] array */
csvRows.push(headers.join(','));
// Loop to get value of each objects key
for (const row of data) {
const values = headers.map(header => {
const val = row[header]
return `"${val}"`;
});
// To add, separator between each value
csvRows.push(values.join(','));
}
/* To add new line for each objects values
and this return statement array csvRows
to this function.*/
return csvRows.join('\n');
};
const data = [{
"firstname": "geeks",
"lastname": "org",
"age": 12
},
{
"firstname": "devendra",
"lastname": "salunke",
"age": 31
},
{
"firstname": "virat",
"lastname": "kohli",
"age": 34
},
];
// Data passed as parameter
const csvData = objectToCsv(data);
console.log(csvData);
</script>
Output:
firstname,lastname,age
"geeks","org","12"
"devendra","salunke","31"
"virat","kohli","34"
Similar Reads
How to convert a 2D array to a comma-separated values (CSV) string in JavaScript ? Given a 2D array, we have to convert it to a comma-separated values (CSV) string using JS. Input:[ [ "a" , "b"] , [ "c" ,"d" ] ]Output:"a,b c,d"Input:[ [ "1", "2"]["3", "4"]["5", "6"] ]Output:"1,23,45,6"To achieve this, we must know some array prototype functions which will be helpful in this regard
4 min read
How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find
5 min read
How to Convert String to Array of Objects JavaScript ? Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read
How to Convert Array into Array of Objects using map() & reduce() in JavaScript? An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c
2 min read
How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript? In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approa
2 min read