-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?

2
  • Where's the code? and that's not array, it's an object Commented Dec 3, 2015 at 7:19
  • Just FYI that's not a multi-dimensional array - it's an array of objects. Commented Dec 3, 2015 at 7:50

2 Answers 2

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
              });
})
Sign up to request clarification or add additional context in comments.

Comments

0

See some examples here and read some more about JSON

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.