I'm looping through an object in javascript and deleting an item that is undefined, using:
for (var key in result) {
if (result.hasOwnProperty(key)) {
var obj = result[key];
if (typeof obj.name === 'undefined') {
delete result[key];
}
}
}
If I don't use the delete , this iterates just fine. However, when I use delete, I then get the error, 'TypeError: Cannot read property 'name' of undefined'
Any idea what I'm doing wrong here?
Thank you
EDIT: The object being iterated:
{
date: Mon, 02 Apr 2012 17: 48: 17 GMT,
t_date: Mon, 02 Apr 2012 17: 48: 17 GMT,
start: 0,
_id: 4f79e661d7cb8ccc1f000005
} {
date: Mon,n02 Apr 2012 17: 48: 26 GMT,
t_date: Mon, 02 Apr 2012 17: 48: 26 GMT,
start: 0,
_id: 4f79e66ad7cb8ccc1f000006
} {
name: 'testname',
date: Mon, 02 Apr 2012 17: 48: 29 GMT,
t_date: Mon, 02 Apr 2012 17: 48: 29 GMT,
start: 0,
_id: 4f79e66dd7cb8ccc1f000007
}
new Date(), this is the result of a query from MongoDB. This is using the nodejs mongodb adapter.