1

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
}
4
  • could you paste in what this object you're iterating over looks like? Commented Apr 2, 2012 at 21:01
  • Added it above - this is results from MongoDB Commented Apr 2, 2012 at 21:11
  • Are those actual date objects? Or are you trying to store dates in JSON? (because you can't do that, you need to use a string) Commented Apr 2, 2012 at 21:12
  • Those are MongoDB date types, their inserted into a date type using new Date(), this is the result of a query from MongoDB. This is using the nodejs mongodb adapter. Commented Apr 2, 2012 at 21:15

2 Answers 2

6

It means that obj is undefined and therefore obj.name causes this error.

It should be:

 var obj = result[key];
 if (obj && typeof obj.name === 'undefined') {
    delete result[key];
 }
Sign up to request clarification or add additional context in comments.

5 Comments

This is most likely it. You will have a property in result that is undefined. Something like {a:1, b:undefined, c:3} will fail on b
Didn't work, same issue. I added an example of the data iterating through above, some objects contain the property name, and some don't.
Are you sure the error happens in this piece of code? Did you try to debug the code and find out in what line it crashes? What if you try plain if (obj && !obj.name) {...} ?
If I do a console.log(result[key]) in place of the delete result[key], it displays the objects that are missing a name property. No errors. It's when I add the delete, that then I get the error, like it's deleting the whole object 'result', not result[key]....
Works for me: jsfiddle.net/spCum/4 Can you share an example that fails like I just did? I.e. edit the example and updated.
-1

I'm not 100% sure why you're using the typeof operator there, but I think you can simplify the if statement to simply:

if(obj === undefined)

I also think the hasOwnProperty check is redundant, not sure what you are checking for there.

I've created an example to demonstrate this here: http://jsfiddle.net/andrewferrier/RxTF8/ (just use your browser console to see the resultant object).

3 Comments

He isn't checking if obj is undefined, he is checking if it has an undefined name property. The hasOwnProperty check is important if you, or one of the libraries you're using, modifies existing prototypes. It's usually best to leave it in there.
typeof is used just in case someone overwrites undefined (because you can do that =/). hasOwnProperty is there so you don't iterate over properties from up the prototype chain (Object.prototype).
Ah, I obviously misunderstood the question. I took "an item that is undefined" to be checking for the value of each property. Thanks for the clarification on hasOwnProperty.

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.