2017-08-31 8 views
1

オブジェクト私はこのオブジェクトを取得する必要があります。変数はリテラル

{ 
    "1": { 
    "epg": { 
     "title1": "TITLE", 
     "start1": "10:00", 
     "stop1": "12:50", 
     "description1": "DESC", 
     "percent1": "17", 
     "step1": "2" 
    } 
}, 
    "2": { 
    "epg": { 
     "title2": "TITLE", 
     "start2": "13:50", 
     "stop2": "14:00", 
     "description2": "DESC", 
     "percent2": "-262", 
     "step2": "7" 
    } 
    } 
} 

私はこのコードを書く:

/* MAKE - 5 shows epg */ 
for (i = 0; i < 2; i++) { 
    data.push({i: {"epg": {"title1": "", "start1": "", "stop1": "", "description1": "", "percent1": "", "step1": ""}}}); 
}; 

/* SEND - 5 shows epg */ 
res.json(data); 

を、私はこの出力を得る:

[{"i":{"epg":{"title1":"","start1":"","stop1":"","description1":"","percent1":"","step1":""}}},{"i":{"epg":{"title1":"","start1":"","stop1":"","description1":"","percent1":"","step1":""}}}] 

それは間違っています...あなたは私に機能のヒントや方法を教えてください。

+1

'データ[I] = {EPG:...};これは私は番号を割り当てるask..howを忘れiはneed..butどの一つである' –

答えて

0

オブジェクトを使用し、インクリメントされたインデックスを割り当てることができます。

var data = {}, 
 
    j; 
 

 
for (i = 0; i < 2; i++) { 
 
    j = i + 1; 
 
    data[j] = { epg: {} }; 
 
    data[j].epg['title' + j] = ''; 
 
    data[j].epg['start' + j] = ''; 
 
    data[j].epg['stop' + j] = ''; 
 
    data[j].epg['percent' + j] = ''; 
 
    data[j].epg['step' + j] = ''; 
 
} 
 

 
console.log(data);

+0

おかげそれは次のように見える:title1 title2 title3 "title" + iを使ってみるが、エラーメッセージが出る予期せぬトークン+ – John

関連する問題