2

i like to convert array to json object like this

var obj = [{item:'name1',start:new date()}, {item:'name2',start:new date()},{item:'name3',start:new date()}]

i am using single dimension array means is working fine. check this link http://jsfiddle.net/H4ezf/1/

 var objectArray= {};    
 objectArray['title']='All Day Event';
 objectArray['start']=new Date(y, m, 1);
 console.log(JSON.stringify(objectArray)); 

output as : {"title":"All Day Event","start":"2012-06-30T18:30:00.000Z"}

but i try to convert list of array to list json object using json stringify like this

var objectArray= {};    
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));

it not working. what i am wrong here. Please any one can help me to solve this problem

3 Answers 3

6

You cannot do this:

var objectArray= {};    
objectArray[0]['title']='name1';

as objectArray[0] does not exist yet. There is no array at that index and therefor you cannot add a string at an index. You have do declare the array first. The rest of your code works just fine.

JSFIDDLE

Sign up to request clarification or add additional context in comments.

Comments

2
var objectArray= [];
objectArray[0] = {}
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1] = {}
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));​

1 Comment

Thank for your answer its working fine. i post this link for others jsfiddle.net/H4ezf/2 - this one i need
1

try it like this:

var objectArray = [];
objectArray[0] = {};
objectArray[0]['title'] = 'name1';
objectArray[0]['start'] = new Date();

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.