1

I have a CSV file I am working with. I don't want to convert it to an object to have keys and values. I want to be able to create more arrays to store different values I.e Temperature Hum hours snowDepth etc All these will be arrays with 7 values

Right now, this is how the csv is stored in the data array

  1. "11/28/2018 7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4" ​
  2. "11/28/2018 7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72" ​
  3. "11/28/2018 8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98" ​
  4. "11/28/2018 8:15,1131,-2.872,88,34.55,1.073,306.6,25.65" ​
  5. "11/28/2018 8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06" ​
  6. "11/28/2018 8:45,1133,-3.064,93.3,35,1.449,312.6,30.13" ​
  7. "11/28/2018 9:00,1134,-2.771,91.5,33.44,0.988,311,28.27" ​ length: 7

the 1st col is the timestamp, 2nd is record number, 3rd is the temperature, 4th is the humidity and so on.

function get_Data(data) {
    "use strict";
    var fileData = new Array();
    data = data.split(/\r\n|\r|\n/);
    data = data.slice(Math.max(data.length - 7, 0));
    createGraph(data);
} 

I want arrays like this

Tem = [-2.122, -2.325, -2.679, -2.872 ...]

hours = [7:30, 7:45, 8:00, 8:15 ...]

3
  • You don't really have to parse anything. You just have to split by comma. To create an array of the values of column i: values = arr.map(str => str.split(',')[i]). Lots of related questions: stackoverflow.com/search?q=%5Bjavascript%5D+parse+csv Commented Jan 16, 2019 at 23:35
  • Could you add every value? We have the first four, but in each row there are eight values. Commented Jan 16, 2019 at 23:42
  • yeah, the 5th is SnowDepth, 6th is WS_ms_S_WVT(speed) 7th is WindDir_D1_WVT and 8th is the SD Commented Jan 17, 2019 at 0:05

3 Answers 3

2

Simple - map and split for each comma, then look at the index for which item it should be:

var data = ["11/28/2018 7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4",
  "11/28/2018 7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72",
  "11/28/2018 8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98",
  "11/28/2018 8:15,1131,-2.872,88,34.55,1.073,306.6,25.65",
  "11/28/2018 8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06",
  "11/28/2018 8:45,1133,-3.064,93.3,35,1.449,312.6,30.13",
  "11/28/2018 9:00,1134,-2.771,91.5,33.44,0.988,311,28.27",
];

var items = {
  date: [],
  recordNumber: [],
  temperature: [],
  humidity: [],
  value5: [],
  value6: [],
  value7: [],
  value8: [],
};

Object.keys(items).forEach((key, index) => items[key].push(data.map(str => str.split(",")[index])));

console.log(items);

Sign up to request clarification or add additional context in comments.

Comments

0

You're mixing up a few things. What's fileData doing (seems unused)? And slice isn't the right way to do this.

Instead, try:

function get_Data(csv) {
    csv = csv.replace(/[\r\n]+/gi,"\n"); // reduce line endings into single LF chars
    rows = csv.split("\n"); // split into array of rows
    var result = [];
    for (var i in rows) result.push(rows[i].split(",")); // split each row
    return result;
}

(edit) If you only the need the last X rows of the CSV data, use:

function get_Data(csv,lastNumRows) { // get only the last <lastNumRows> rows
    csv = csv.replace(/[\r\n]+/gi,"\n"); // reduce line endings into single LF chars
    rows = csv.split("\n").slice(-lastNumRows); // split into array of rows
    var result = [];
    for (var i in rows) result.push(rows[i].split(",")); // split each row
    return result;
}

3 Comments

That's true, I don't need the fileData. I wanted the last 7 rows of the file
Right, now I see how you used slice. I edited my answer for getting only the last 7 rows.
Oh but I also see I misread your original question, you actually need the columns (e.g. all temperatures or all humidities), not rows. My bad.
0

try this, it should work. i know the position of the data so it was easy to split and find the terms and date

var csv = document.getElementById("csvFile").innerHTML ;



function get_Data(data) {
    var result = {terms:[], hours:[] }
    var r = data.split(/\r\n|\r|\n/);

    r.forEach(function(element) {
    if (element !== "" && element.length>1){
      var term = element.split(",")[2]
      var date = new Date(element.split(",")[0])
      var hour =  date.getHours() + ":" + (date.getMinutes()<=9 ? "0" + date.getMinutes() : date.getMinutes());
      
      result.terms.push(term);
      result.hours.push(hour)
      }
    });
 return result;
} 

console.log( get_Data(csv))
<div id="csvFile" style="display:none;">
11/28/2018 7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4
11/28/2018 7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72
11/28/2018 8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98
11/28/2018 8:15,1131,-2.872,88,34.55,1.073,306.6,25.65
11/28/2018 8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06
11/28/2018 8:45,1133,-3.064,93.3,35,1.449,312.6,30.13
11/28/2018 9:00,1134,-2.771,91.5,33.44,0.988,311,28.27
</div>

2 Comments

I don't want to do position because the records varies each time there is a new row. This will affect the values stored in an array. I don't know if that makes sense
hmm but @Jack Bashford did the same thing. my solution is like this becouse then you could control your data parsing. like hour for instead. but yah as you which

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.