我正在構建一個對象數組。結構應該是這樣的:JS對象中包含的數組嵌套兩深
var array = [
{ 'keyword' : 'somekeyword',
'guidelines' : [ "guideline1", "guideline2", "guideline3"]
},
{ 'keyword' : 'anotherkeyword',
'guidelines' : [ "guideline1", "guideline2", "guideline3"]
}
]
相反,它是這樣的:
var array = [
{ 'keyword' : 'somekeyword',
'guidelines' : [[ "guideline1", "guideline2", "guideline3"]]
},
{ 'keyword' : 'anotherkeyword',
'guidelines' : [[ "guideline1", "guideline2", "guideline3"]]
}
]
我所建立我的對象,以得到嵌套的指導方針陣列上的是第二級做錯了什麼?這是構建陣列,在一個HTML文件,從數據表中讀取的功能:
$(html).find('tr').each(function() {
keywordObj = {};
guidelinesCell;
$(this).find('td').each(function() {
cellClass= $(this).attr('class');
if (cellClass === 's2') {
keywordObj.keyword = $(this).text();
} else if (cellClass === 's3' || cellClass == 's4') {
guidelinesCell = [];
guidelinesCell.push($(this).html().split('<br>'));
//Possibly it's this push that's causing the problem?
keywordObj.guidelines = guidelinesCell;
console.log(guidelinesCell)
console.log(keywordObj.guidelines)
//At this point, the array is already nested- the console logs return eg
[Array[2]]
0: Array[2]
0: "GuidelineName1.pdf"
1: "GuidelineName2.pdf"
}
});
i ++;
if (keywordObj.guidelines && keywordObj.guidelines[0].length > 1) {
keywordsToLeitlinien.push(keywordObj);
}
});
僅供參考,這裏是加價的樣本它是從閱讀:
<tr dir="ltr">
<td class="hd">
<p style="height:39px;">.</p>
</td>
<td dir="ltr" class="s2">samplekeyword</td>
<td dir="ltr" class="s4">GuidelineName.pdf <br>GuidelineName.pdf</td>
</tr>
編輯補充:
這是不是在這個文件中我處理的問題,但我最終輸出數組到JSON文件,我寧願解決這個問題比保持爲解決此排序輸出:
{"keyword":"keywordname","guidelines":[["guidelineName1 ","guidelineName2"]]}
你說的沒錯,你會得到的是最快的正確答案蜱(當我允許接受的答案)。乾杯! – Ila