-2

I need to create an array like this:

events: [
    id   :'1',
    title: 'All Day Event'
},{
    id   : '2',
    title: 'some name'
},{
    id: 999,
    title: 'some title',
}]

I am executing this code inside a loop:

$events['title'] = 'hello';
$events['id'] = '1';

It's returning:

[ title: "hello", id: "1" ]
[ title: "hello", id: "1" ]

How can I change the code to meet my requirement?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Niroj Adhikary
  • 1,775
  • 18
  • 30

2 Answers2

1

Try:

    var id1 = '1';
    var title1 = 'All Day Event';
    var events = [];
    events.push({
            id   :id1,
            title: title1
          });

for a loop:

titles = ['All Day Event1','All Day Event2'];
ids = ['1','2'];//note both array need to have the same length

$.each(titles,function(i,v){
 events.push({
                id   :ids[i],
                title: titles[i]//or v
              });
})
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

See some examples here and read some more about JSON

You need to iterate the JSON while adding dimensions and values in it

Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94