0

I have an object that is evaluating as undefined when I try to read its keys using Object.keys(). However, when I do Object.keys().length I get 1.

How can I test an object for presence if any key other than undefined or null.

I've tried the following, but still get errors. I'm essentially just checking the first element to see if it is null or undefined. I'm guessing there may be a better way to run this kind of check.

I've googled and seen cases of handling a search for a specific property, but that is not how my object is setup. I actually have an object with nested objects. Player1 and Player2 can be any value.

var data =
    { 
      player1:
       { team: team1,
         score: 6 },
      player2:
       { team: team2,
         score: 4} 
    }

Here is what my object can also look like and it is causing undefined to be displayed when I attempt to loop through and display it in a table.

{ undefined:
   { team: undefined,
     score: '0'} }

As you can see doing a length check will return 1. So I'm guessing I have to check for the first elemnent data[0]? That check works, but is that the best way to handle this?

if (Object.keys(data[0]) === null){
do something
}
6
  • 3
    data[0] is undefined. What are you trying to do? Commented Jan 24, 2018 at 6:13
  • 1
    "Javascript test if object has any keys that are not undefined or null" The keys (property names) in an object will never be undefined or null. Property names are always strings or Symbols. The question is really unclear, please do reply to @mehulmpt's question above. Commented Jan 24, 2018 at 6:19
  • I've added more detail to the question. data[0] is indeed undefined. Is checking for data[0] the only way to find out if data does not contain any elements? My assumption was that I could do if (Object.keys(data[0]) === null){ do something } Commented Jan 24, 2018 at 6:32
  • I selected Arash's answer because it addressed my question. Getting the length on my object as shown in my question was giving me a value of 1. The test that worked for me was doing a test on data itself. I did not think to do that because my assumption was that if I am getting a value greater than 0 on the object length, then doing a test on the object itself would not give false. Commented Jan 24, 2018 at 6:45
  • @mo_maat: "data[0] is indeed undefined" Right, but that's not the property key. That's the property value (or rather, the value you get when the object doesn't have a property by the name you requested). The property key you're checking there is "0". Since data doesn't have a property called "0", you get undefined. Again: Keys cannot be undefined, null, or indeed anything but a string or Symbol. Commented Jan 24, 2018 at 6:59

2 Answers 2

5
if (Object.keys(data).length === 0){
    // There are no keys
}

Also check that data itself isn't null or undefined.

if (!data || typeof data !== "object" || Object.keys(data).length === 0) {
    // This is either a null, undefined, not an object, or an object with no keys
}
Sign up to request clarification or add additional context in comments.

1 Comment

Won't work for {one: undefined, two: undefined}
4

I have an object that is evaluating as undefined

because data[0] is undefined

Directly use

if ( Object.keys( data ).length == 0 )
{
   do something
}

2 Comments

data[0] is undefined, not null
@mehulmpt You are right about that, I have fixed the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.