Creating a CSV File for Download from a JavaScript Array

I have the information stored in a specific structure:

var nicedata =[{...}, {...}, {...}, {...}, {...}, {...}, {...}, {...}, {...}, {...}]

each individual object has this format:

0:{1: "1", Affiliate Name: "AName", Brand1: "Bname", Business type: "Company Stores", Calendar Month Year: "mm/dd/yyyy", Category: "Null", ...}

1:{1: "1", Affiliate Name: "AName", Brand1: "Bname", Business type: "Company Stores", Calendar Month Year: "mm/dd/yyyy", Category: "Null", ...}

I am attempting to export this data into a CSV file, but my current attempt using alaSQL is not producing the desired output.

alasql("SELECT * INTO CSV('DataExport.csv',{headers:true}) FROM ?", [niceData]); 

Although I can successfully download the data, each row's entire content is being inserted into a single cell.

My goal is to generate a proper CSV file with headers and corresponding values. Can anyone provide assistance with achieving this?

Answer №1

Imagine a comma-separated text as nothing more than a transformed table. This transformation can be achieved by using a combination of for loops, as demonstrated in the code snippet below.

var array = [{
 a:'11',
 b:'12',
 c:'13'
}, {
 a:'21',
 b:'22',
 c:'23'
}, {
 a:'31',
 b:'32',
 c:'33'
}];

var csv = '';
for(var row of array){
  for(var prop in row){
    var cellVal = row[prop];
    csv += '"' + cellVal + '", '
  }
  csv += '\n';
}

console.log(csv);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for inserting a variable into an attribute value when creating it using TypeScript

this.listvalue; list.forEach((v: any) => { v.data.forEach((v2: any) => { this.searchlist.push({ header: v.header, value: v2.value_0 }); }); }); Is there a way to replace v2.value_0 with v2.this.listvalue? I've tried ...

Is there a way to change ngx-timeago language when the button is pressed?

I was trying to implement ngx-timeago localization. Everything seems to be working fine, but I am struggling with changing the language from German to Spanish when a button is pressed. Here's the template I am using: {{ date | timeago:live}} <div ...

Trouble retrieving information from cloud function through https callable connection

I've encountered an issue with my Angular application where the value returned from a cloud function is always null, despite the fact that the cloud function logs the correct result. I'm unsure of what mistake I might be making. My Angular code ...

What's the scoop on NodeJS, Express, Nginx, and Jade?

Currently exploring options for technologies and libraries to use in a new, large-scale project. With knowledge of NodeJS, JavaScript, Express, and Jade (now Pug), my team and I are leaning towards adopting these for the project. The main issue we're ...

Executing multiple commands within a single child process in Node.js

Is it possible to create a new process in node.js and execute multiple terminal shell commands along with running node.js functions like fs.writeFileSync within the same context? For instance, I would like to perform the following tasks within a single pr ...

What methods can I use to evaluate my Angular scope functions in karma and jasmine?

I recently started learning Angular and am new to Jasmine testing. I have a function in my controller that adds an object from JSON data into an empty array. My controller with the cart-related functions: $scope.cart = []; $scope.addItemToCart = funct ...

Utilizing SVG icons with AngularJS for optimal compatibility in Firefox

When integrating SVG icons with AngularJS and enabling html5Mode, it is essential to set the basePath in the html document's head. <base href="/"> To utilize SVG icons, all symbols need to be loaded into the index.html page like so: <?xml ...

"Mastering the art of event delegation: A guide to effectively engaging with various

I have data that is generated dynamically, as shown in the snippet below: const resultsDiv = document.getElementById("results"); const getList = document.getElementById("example"); document.querySelector(".container").addEventListener("click", function ...

Having difficulty resetting the form post-login in Ionic

I found this code snippet on a blog post about validation in Ionic apps, you can check it out here While working on designing a login form in Ionic, I encountered two issues: 1. I'm confused about why there are 2 submits used in the example: <fo ...

Discover the nodes with the highest connections in a D3 Force Graph

As I explore the functionalities of a D3 Force Directed Graph with zoom and pan features, I encounter an issue due to my limited knowledge of d3.js. Is there a way to estimate the number of links for each node in this scenario? I am currently at a loss on ...

What is the reason for recursion not producing a new object as output?

Trying to filter out nodes in a recursion function that iterates through a tree based on the registry property. function reduceNodesRegistry(source: any) { if (!source.registry) return source; return { ...source, children: s ...

What is the formula to determine the sizes of grandchildren div containers?

Hey everyone, I'm having some trouble with my jQuery code and I can't seem to figure out what's going on. Here's the scenario: I'm creating a few divs (based on entries from MySQL) and I'd like to show you the end result: ...

Using JQuery to extract information from a JSON file

Here is the code I am working on, where I pass input username and password values. The function I have written checks if the input matches the data in a JSON file. If there is a match, an alert saying "login correct" will be displayed, otherwise it will di ...

Tips for managing multiple controlled tooltip components in Material UI

I recently started working on Material UI with React for my project and I'm still learning the ropes. For every item in my list, I want a tooltip that displays relevant content. I am using the MAP method to generate the list items. Currently, the to ...

Implementing selective input element disabling within a form using jQuery

In an attempt to deactivate the majority of input elements within a form using jQuery, I have encountered a challenge. While most input elements should be disabled, there are select ones that need to remain enabled. Here is my current code snippet: $(docu ...

Trouble with Express.js and Mongoose: Issue with find() method not returning results for specified variable

For the task I'm working on, my goal is to display items that are only visible to the user who posted them. To manage sessions, I am using Passport.js. The code I have written seems to be functioning well up until the end. app.get('/latestp ...

Matching current URL and links by checking the div class

How can I verify if the current URL matches a link on my page, but also check if that link has a specific div class? For example: jQuery(document).ready(function($){ // find anchor tag on page with matching current location URL jQuery("*").find("a[h ...

JavaScript function to add or subtract

I need to include additional inputs for "+ and -" 60mm and 120mm in my function. How can I achieve this without turning all of them into separate functions? <template> <card class="card"> <h2>Measurement</h2> <form&g ...

Adding a dynamic row to ag-grid with customizable columns on the fly

My goal is to dynamically add a new row when a button is clicked in the ag-grid on my ReactJS page. The code below works for me when I know the fixed number of columns at design-time. let row ={ products1: "a", products2: "b", products ...

The barcode is not displaying when using javascript:window.print() to print

I am currently developing a Mean Stack App where I have a requirement to display a barcode. To achieve this, I am utilizing an AngularJS directive for generating a 128 barcode, and it is being generated successfully. However, when I attempt to print by cli ...