JSFiddle - React, Tailwind, and code Playground
by ronilan
HTML
Data:
<pre class="data">
"ID";"Date";"Time";"Status";"value"
"1";"2013.01.01";"10:13 AM";"Active";"19"
"2";"2013.01.05";"07:45 AM";"Active";"7"
"3";"2013.01.03";"9:12 PM";"Active";"7"
"4";"2010.01.01";"6:37:02 PM";"Inactive";"723"
"5";"2010.01.11";"1:57 AM";"Inactive";"1273"
"6";"2013.01.03";"9:12 PM";"Active";"733"
"10";"2010.01.01";"6:37:01 PM";"Inactive";"223"
</pre>
Highest Value:
<pre id="highestValue"></pre>
Earliest Time:
<pre id="earliestTime"></pre>
All Active:
<pre id="allActive"></pre>
JavaScript
String.prototype.stripEnclosingQuotes = function () {
return this.replace(/^"|"$/g, '');
}
/**
rowToObject turns a data row into a JS object.
@param {Array} rowArray, {Array} headerArray
@return {Object} with key value
*/
function rowToObject(rowArray, headerArray) {
var result = {},
i,
max = headerArray.length,
key,
value;
for (i = 0; i < max; i++) {
value = rowArray[i].stripEnclosingQuotes();
key = headerArray[i].stripEnclosingQuotes();
result[key] = value;
}
return result;
}
/**
csvToArray turns a csv multiline string into an array of objects
@param {string} csvString, {string} delimiter
@return {Array} of objects
*/
function csvToArray(csvString, delimiter) {
var result = [],
csvRows = csvString.split(/\n/),
csvHeaders = csvRows.shift().split(delimiter),
i,
max,
rowArray;
max = csvRows.length;
for (i = 0; i < max; i++) {
rowArray = csvRows[i].split(delimiter);
result[i] = rowToObject(rowArray, csvHeaders);
}
return result;
}
function highestValue(array) {
var result = array[0],
i,
max = array.length;
for (i = 0; i < max; i++) {
if (parseInt(array[i].value) > result.value) {
//result = parseInt(array[i].value);
result = array[i];
}
}
return result;
}
function earliestTime(array) {
var result = array[0],
i,
max = array.length,
d,
dResult = new Date(array[0].Date);
for (i = 0; i < max; i++) {
d = new Date(array[i].Date + " " + array[i].Time );
if (d < dResult){
//result = d;
dResult = d;
result = array[i];
}
}
return result;
}
function allActive(array) {
var result = [],
i,
max = array.length;
for (i = 0; i < max; i++) {
if (array[i].Status === "Active") {
...