我正在解析一个 csv 文件。我的目标是查找以“A”、“B”开头的名称并计算出现次数。以下代码的问题是它永远不会完成while
循环。
HTML:
<form>
<input type="button" id="csv1ButtonRun" value="Run" style="background-color: lightgray"/>
</form>
脚本包括:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="app.js"></script>
<script src="jquery.csv-0.71.js"></script>
JavaScript:
var babyCsv = " Jacob, 1, boy, 2010, Isabella, 1, girl, 2010, Ethan, 2, boy, 2010, Sophia, 2, girl, 2010, Michael, 3, boy, 2010, Emma, 3, girl, 2010, Jayden, 4, boy, 2010, Olivia, 4, girl, 2010, William, 5, boy, 2010, Ava, 5, girl, 2010, Alexander, 6, boy, 2010, Emily, 6, girl, 2010, Noah, 7, boy, 2010, Abigail, 7, girl, 2010, Daniel, 8, boy, 2010, Madison, 8, girl, 2010, Aiden, 9, boy, 2010, Chloe, 9, girl, 2010, Anthony, 10, boy, 2010, Mia, 10, girl, 2010, Joshua, 11, boy, 2010, Addison, 11, girl, 2010, Mason, 12, boy, 2010, Elizabeth, 12, girl, 2010, Christopher, 13, boy, 2010, Ella, 13, girl, 2010, Andrew, 14, boy, 2010, Natalie, 14, girl, 2010, David, 15, boy, 2010, Samantha, 15, girl, 2010, Matthew, 16, boy, 2010";
var data = $.csv.toArray(babyCsv);
$("#csv1ButtonRun").click(function() {
var arrayLength = data.length / 4;
var count1 = 0;
var count2 = 0;
var i = 0;
while (i <= arrayLength) {
var name = data[i * 4];
if (name.substring(0, 2) == " A") {
count1 = count1 + 1;
}
else if (name.substring(0, 2) == " B") {
count2 = count2 + 1;
};
i++;
};
document.write(count1 + count2);
});