因此,这里有csv2arr,它将您的CSV字符串转换为如下所示的对象: { date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833} 和stats,它们采用类似的对象数组来为每一列查找统计信息,例如maxVal和minRow.如果需要,我们可以增强它以查找其他统计信息,例如mean和median.其输出如下所示: { date: { /* ... */ }, open: { /* ... */ }, high: { /* ... */ }, low: { /* ... */ }, close: { minVal: 9199.584833, maxVal: 9427.687584, minRow: {date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833}, maxRow: {date: "29/10/2019", open: 9248.440562, high: 9516.181048, low: 9232.648086, close: 9427.687584} }} 这是一种方法: // Naive, but useful for some dataconst csv2arr = (csv) => { const [headers, ...rows] = csv .trim () .split ('\n') .map (r => r .split (',')) return rows .reduce ((a, r) => [ ... a, Object .assign (... (r .map ((c, i) => ({[headers [i]]: isNaN(c) ? c : Number(c)})))) ], [])}const stats = (rows) => rows .reduce ( (stats, row) => Object .assign ( ... Object .entries (row) .map (([key, val]) => ({[key]: ({ minVal: key in stats && stats [key] .minVal < val ? stats [key] .minVal : val, maxVal: key in stats && stats [key] .maxVal > val ? stats [key] .maxVal : val, minRow: key in stats && stats [key] .minRow [key] < val ? stats [key] .minRow : row, maxRow: key in stats && stats [key] .maxRow [key] > val ? stats [key] .maxRow : row, })})) ), {})const csv = `date,open,high,low,close31/10/2019,9202.457589,9383.160892,9028.71744,9199.58483330/10/2019,9422.463325,9426.874217,9085.370357,9205.72655929/10/2019,9248.440562,9516.181048,9232.648086,9427.68758428/10/2019,9565.101883,9805.118089,9256.148389,9256.148389`const rows = csv2arr (csv)const statistics = stats (rows)console .log (rows)console .log (statistics)请注意,此处日期的最小值/最大值没有太大意义.如果它们是ISO格式的(例如"2019-10-31"),那么这些值也将获得有意义的最大值和最小值. csv2arr在相当广泛的情况下很有用.但是不要将其误认为是完整的CSV解析器.例如,如果您的数据包含带有逗号的单元格,它将失败.对输出也很幼稚.本质上,即使它看起来像一个数字,它也会变成一个数字,即使该列中的其他内容不同.还有其他问题.但是它对于许多逗号分隔格式仍然有用.I have already found the lowest value in a column in a csv file, but I can't find which row store this value. Can someone help me please? Is there anyway I can find the answer by myself though I have googled for many days and websites. Thanks in advance.function getDataPointsFromCSV(csv) { var dataPoints = csvLines = []; var mini, a var minIndex = 0 csvLines = csv.split(/[\r?\n|\r|\n]+/); for (var i = 1; i <= csvLines.length; i++) if (csvLines[i] > 0) { points = csvLines[i].split(",");//points instead of csvLines mini = points[4] } var a = mini for (var i = 1; i <= csvLines.length; i++) if (csvLines[i] < mini) { points = csvLines[i].split(","); minIndex = i mini = csvLines[i]// Find lowest value in a column5(=points[4])( but how to find the row that store the lowest value, can someone help me please?) lowestv = Math.min(price, lowestv) }// example; mini.length for (var i = mini.length; i <= mini.length+10; i++) if (csvLines[i].length > 0) { points = csvLines[i].split(","); price = points[4] } dataPoints.push({x: new Date(),y: parseFloat(minIndex)}); return dataPoints; } 解决方案 I would break this into two steps. First, we parse the CSV to get an array of objects. Then we iterate over them to find stats like minVal and minRow (and any others we might want.) While this is slightly less efficient than doing it in a single pass, it leads to simpler, more maintainable code.So here we have csv2arr, which converts your CSV string into objects that look like this:{ date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833}and stats, which takes an array of objects like that to find statistics such as maxVal and minRow for each column. We could enhance this to find other stats such as mean and median if desired. Its output looks like this:{ date: { /* ... */ }, open: { /* ... */ }, high: { /* ... */ }, low: { /* ... */ }, close: { minVal: 9199.584833, maxVal: 9427.687584, minRow: {date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833}, maxRow: {date: "29/10/2019", open: 9248.440562, high: 9516.181048, low: 9232.648086, close: 9427.687584} }}This is one way to do so:// Naive, but useful for some dataconst csv2arr = (csv) => { const [headers, ...rows] = csv .trim () .split ('\n') .map (r => r .split (',')) return rows .reduce ((a, r) => [ ... a, Object .assign (... (r .map ((c, i) => ({[headers [i]]: isNaN(c) ? c : Number(c)})))) ], [])}const stats = (rows) => rows .reduce ( (stats, row) => Object .assign ( ... Object .entries (row) .map (([key, val]) => ({[key]: ({ minVal: key in stats && stats [key] .minVal < val ? stats [key] .minVal : val, maxVal: key in stats && stats [key] .maxVal > val ? stats [key] .maxVal : val, minRow: key in stats && stats [key] .minRow [key] < val ? stats [key] .minRow : row, maxRow: key in stats && stats [key] .maxRow [key] > val ? stats [key] .maxRow : row, })})) ), {})const csv = `date,open,high,low,close31/10/2019,9202.457589,9383.160892,9028.71744,9199.58483330/10/2019,9422.463325,9426.874217,9085.370357,9205.72655929/10/2019,9248.440562,9516.181048,9232.648086,9427.68758428/10/2019,9565.101883,9805.118089,9256.148389,9256.148389`const rows = csv2arr (csv)const statistics = stats (rows)console .log (rows)console .log (statistics)Note that the min/max values for dates here don't make much sense. If they were ISO-formatted (e.g. "2019-10-31"), then these values would also get meaningful maximums and minimums.csv2arr is useful for a reasonably wide range of cases. But don't mistake it for a full CSV parser. If your data includes cells with commas, for instance, it will fail. It's also naive about the output. Essentially, if it looks like a number, it becomes a number, even if other things in the column disagree. There are other problems, too. But it's still useful for many comma-delimited formats. 这篇关于查找Math.min行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 09:03