Write a program to convert an array of objects to a CSV string that contains only the columns specified using JavaScript Last Updated : 10 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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" Comment More infoAdvertise with us D devendrasalunke Follow Improve Article Tags : JavaScript Web Technologies javascript-array javascript-object JavaScript-DSA JavaScript-Questions +2 More Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Like