Figuring out what Javascript object is this?
I have some script that has the following code:
var result = {
id: 'test',
name: 'test',
home: 'test',
ex_info: [{date: new Date('12-31-2010'), quantity: 976}],
status: ''
}
I am trying to insert information in the ex_info property. But I can't add anything. I try to hard to code the following:
var result = {
id: 'test',
name: 'test',
home: 'test',
ex_info: [{date: new Date('03-31-2010'), quantity: 976}, {date: new Date('02-31-2010'), quantity: 543}],
status: ''
}
But it results in an error:
Update:
Error: Expected [ { date : Date(Fri Mar 31 2010 00:00:00 GMT-0500 (EST)), quantity : 976 }开发者_开发技巧, { date : Date(Thu Mar 03 2011 00:00:00 GMT-0500 (EST)), quantity : 543 } ] to equal [ { date : Date(Fri Mar 31 2010 00:00:00 GMT-0500 (EST), quantity : 976 } ]
That's the correct way to append item to ex_info array.
Probably the code using "result" does not support more than one item in that array.
Your variable test is undefined. I imagine you meant to encompass that in a string:
var result = {
id: 'test',
name: 'test',
home: 'test',
ex_info: [{date: new Date('03-31-2010'), quantity: 976}, {date: new Date('02-31-2010'), quantity: 543}],
status: ''
}
There are four ways of instantiating a date:
new Date() = current date and time
new Date(ms) = milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
精彩评论