1
我需要有類似的JSON結構:如何在循環中填寫JSON?
{
"group1": ["A", "B", "C"],
"group2": ["C", "D", "E"],
"group3": ["F", "G", "H"]
}
,需要在週期來創建它:
courses.each(function(index) {
name = $(this).attr("href").replace(/([^\/]*)\/.*/, "$1");
prefix = $(this).attr("href").replace(/[^\/]*\/(.*)/, "$1");
if (subjects.indexOf(prefix) == -1) {
subjects[prefix] = new Array();
}
subjects[prefix].push(name);
});
課程變量是DOM對象從某事像這樣:
<a href="group1/A">...
<a href="group1/B">...
<a href="group2/D">...
循環執行後,它的內容如下:
[Array[0], "group1", "group2"]
不是上面提到的結構...
爲什麼?
我不完全相信你的問題是什麼? – 2011-02-07 20:18:27