Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

CSV to javascript array

I have a .CSV file that has 3 columns file that needs to be converted to 3 arrays(one for each column(obviously))

The .CSV is sever side and not client side, if that matters
CSV files are comma separated. It's literally in the name. Do note that sometimes the values between the separators are enclosed in quotation marks in order to prevent the separator from being broken. For more information, there is an entire standard in regards to CSV files that you have to take into account if you want to do this properly:

https://tools.ietf.org/html/rfc4180

Since you're using JavaScript do to this, you can very easily create a function that, according to the CSV standard, takes out all the values and pushes the values into an array:
https://developer.mozilla.org/en-US/docs...Array/push

I don't really know why you need 3 arrays for this? It makes no sense to me. You could very easily push the values into a single JSON object, much like this:
Code
{
  "0": [
    "row0",
    "row1"
    ],
  "1": [
    "row0",
    "row1"
    ],
  "2": [
    "row0",
    "row1"
    ]
}
[Image: vp6iypF.gif]
(Nov 10, 2019 06:03 AM)MavenGradle Wrote:  CSV files are comma separated. It's literally in the name. Do note that sometimes the values between the separators are enclosed in quotation marks in order to prevent the separator from being broken. For more information, there is an entire standard in regards to CSV files that you have to take into account if you want to do this properly:

RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files

Since you're using JavaScript do to this, you can very easily create a function that, according to the CSV standard, takes out all the values and pushes the values into an array:
Array.prototype.push() - JavaScript | MDN

I don't really know why you need 3 arrays for this? It makes no sense to me. You could very easily push the values into a single JSON object, much like this:
Code
{
 "0": [
   "row0",
   "row1"
   ],
 "1": [
   "row0",
   "row1"
   ],
 "2": [
   "row0",
   "row1"
   ]
}

alright, thank you. I needed the data to be printed out into a graph/chart, I was able to find a solution. Thanks anyways, much appreciated. 🎉 🎉
(Nov 10, 2019 06:03 AM)MavenGradle Wrote:  CSV files are comma separated. It's literally in the name. Do note that sometimes the values between the separators are enclosed in quotation marks in order to prevent the separator from being broken. For more information, there is an entire standard in regards to CSV files that you have to take into account if you want to do this properly:

RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files

Since you're using JavaScript do to this, you can very easily create a function that, according to the CSV standard, takes out all the values and pushes the values into an array:
Array.prototype.push() - JavaScript | MDN

I don't really know why you need 3 arrays for this? It makes no sense to me. You could very easily push the values into a single JSON object, much like this:
Code
{
 "0": [
   "row0",
   "row1"
   ],
 "1": [
   "row0",
   "row1"
   ],
 "2": [
   "row0",
   "row1"
   ]
}

Well we can also Map the items out into Array instead of pushing:

Maybe say  like this?

PHP Code

<?php 
export 
default {
 
 data() {
 
   return {
 
     articles: [],
 
     users: [],  //ignore
 
     category: []
 
   };
 
 },
 
 created() {
 
   let uri = "/api/v1/articles";    //can also be a csv file

 
   axios.get(uri).then(response => {
 
     this.articles = response.data;

 
     console.log(this.articles);

 
      response.data.map(element => {
 
       console.log(element);
 
     });
 
   });
 
 }
}; 


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by flexing_99Apr 29, 2023 12:02 AM 3 270 Apr 29, 2023 12:02 AM
Last Post: Zgh600
[Javascript]  by FacewipeSep 7, 2021 03:44 AM 1 177 Sep 7, 2021 03:44 AM
Last Post: kitkatas
[Javascript]  by Button5Aug 8, 2021 07:32 AM 1 329 Aug 8, 2021 07:32 AM
Last Post: MavenGradle