1

I need to get this object:

{
  "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"
    }
  }
}

I write this code:

/* 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);

And i get this output:

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

And that is incorrect...could you please give me hint of function or how to do it.

John
  • 1,521
  • 3
  • 15
  • 31

1 Answers1

0

You could use an object and assign with the incremented index.

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks this is what i need..but one thing i forget to ask..how to assign numbers so that it looks like this: title1 title2 title3 i try using "title"+i but i get error message Unexpected token + – John Aug 31 '17 at 13:35