你好我做了一个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>
答案 0 :(得分:2)
正如上面提到的@ Beetroot-Betroot,你遇到的问题是你有一个名为word的单个键的对象,每次你用最新的匹配替换该键的值。因此,您将始终只保存最后一个单词。
要匹配评论中列出的结构,您需要使用一组对象。
因此,您应该初始化数组而不是对象:
即:var obj7 = [];
然后,每次匹配单词长度时,都应该向该数组添加一个对象。您可以使用以下内容执行此操作:obj7.push({"word":words[i]});
然后,当您对数组(obj7
)进行字符串化时,您将获得所需的结构。