Trying to find an Object key with a for...in loop!

Tell us what’s happening:

I can’t quite understand why this code does not work?

  **Your code so far**

function orbitalPeriod(obj) {
  for(const key in obj) {
    if(obj.hasOwnProperty(key) === "name") {
      console.log("found!")
    }
  }
}

orbitalPeriod({name: "iss", avgAlt: 413.6});

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Map the Debris

Link to the challenge:

what is the output of this?

Should be…

if(key === “name”) {…}

right?

Follow up question…

Is there a way to do anything to that ‘key’ now that I have matched it.
For example… when I try to delete the key that matched the name prop, it doesn’t work…

if(key === “name”) {
delete key;
}

key is a string, do you want to delete a property from the object? then you need to use the right notation to identify that property

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.