你好,我做了一个 jquery 函数,它根据长度对单词进行排序,而且它很好。但是现在我想将这些单词存储在一个对象中,这样我就可以使用 JSON.stringify 来创建一个 JSON 字符串。问题是我只得到显示为 json 对象的最后一个单词,而不是所有单词。我认为这是因为我需要向对象添加或附加每个单词,但我不知道如何并且还没有找到答案。
这是我的功能:
function sortWords() {
var count7 = 0;
var count8 = 0;
var count9 = 0;
var count10 = 0;
var count11 = 0;
var count12 = 0;
var obj7 = {};
var obj8 = {};
var obj9 = {};
var obj10 = {};
var obj11 = {};
var obj12 = {};
//Get the text from the div
var str = $("div.someText").text();
//Chance everything exept words into spaces
str = str.replace(/[^a-zA-Z]/g," ");
//Replace all spaces for one space
str = str.replace(/ +/g," ");
//Make all words lowercase
str = str.toLowerCase();
//Make an array
var words = str.split(" ");
//Loop trough the array to show the result
for(var i=0;i<words.length;i++){
//sort words by length
switch(words[i].length) {
case 7:
count7++;
$('td.length7').append("<div>"+count7+": "+words[i]+"</div>");
obj7["word"] = words[i];
break;
case 8:
count8++;
$('td.length8').append("<div>"+count8+": "+words[i]+"</div>");
obj8["word"] = words[i];
break;
case 9:
count9++;
$('td.length9').append("<div>"+count9+": "+words[i]+"</div>");
obj9["word"] = words[i];
break;
case 10:
count10++;
$('td.length10').append("<div>"+count10+": "+words[i]+"</div>");
obj10["word"] = words[i];
break;
case 11:
count11++;
$('td.length11').append("<div>"+count11+": "+words[i]+"</div>");
obj11["word"] = words[i];
break;
case 12:
count12++;
$('td.length12').append("<div>"+count12+": "+words[i]+"</div>");
obj12["word"] = words[i];
break;
}
}
jsonStr7 = JSON.stringify(obj7);
$("div.result1").text("The JSON String: " + jsonStr7);
jsonStr8 = JSON.stringify(obj8);
$("div.result2").text("The JSON String: " + jsonStr8);
jsonStr9 = JSON.stringify(obj9);
$("div.result3").text("The JSON String: " + jsonStr9);
jsonStr10 = JSON.stringify(obj10);
$("div.result4").text("The JSON String: " + jsonStr10);
jsonStr11 = JSON.stringify(obj11);
$("div.result5").text("The JSON String: " + jsonStr11);
jsonStr12 = JSON.stringify(obj12);
$("div.result6").text("The JSON String: " + jsonStr12);
};
的HTML:
</head>
<body>
<input onClick="sortWords()" type="button" value="Sorteren op lengte" />
<table border="1" padding="10">
<tr>
<td>length7</td>
</tr>
<tr>
<td class="length7"></td>
</tr>
</table>
<table border="1" padding="10">
<tr>
<td>length8</td>
</tr>
<tr>
<td class="length8"></td>
</tr>
</table>
<table border="1" padding="10">
<tr>
<td>length9</td>
</tr>
<tr>
<td class="length9"></td>
</tr>
</table>
<table border="1" padding="10">
<tr>
<td>length10</td>
</tr>
<tr>
<td class="length10"></td>
</tr>
</table>
<table border="1" padding="10">
<tr>
<td>length11</td>
</tr>
<tr>
<td class="length11"></td>
</tr>
</table>
<table border="1" padding="10">
<tr>
<td>length12</td>
</tr>
<tr>
<td class="length12"></td>
</tr>
</table>
<br />
<div class="array"></div>
<br />
<div class="result1"></div>
<br />
<div class="result2"></div>
<br />
<div class="result3"></div>
<br />
<div class="result4"></div>
<br />
<div class="result5"></div>
<br />
<div class="result6"></div>
<br />
<div class="someText">
a aa aaa aaaa aaaaa aaaaaa aaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaaa
</div>
</body>
</html>