0
我想創建像下面這樣的OBJ:如何在我的情況下創建一個JavaScript數組?
var collection = [{
'title': 'title1',
'content': {
'subtitle': 'sub1',
'contents': 'sub content 1'
}
}, {
'title': 'title2',
'content': {
'subtitle': 'sub2',
'contents': 'sub content 2'
}
}, {
'title': 'title3',
'content': {
'subtitle': 'sub1',
'contents': 'sub content 1'
}
}];
我的數組類似於
var section = [];
for (var i = 0; i < data.length; ++i) {
section['title'] = data.title; //data.title represent title1 to title 3
for (var j = 0; j < data.sections.length; ++j) {
//data.sections[j].content represent {'subtitle':'sub1', 'contents':'sub content 1'}
}
}
我不知道如何調整我的代碼創建OBJ喜歡上面。任何人都可以幫助我嗎?非常感謝!