最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript test if object has any keys that are not undefined or null - Stack Overflow

programmeradmin6浏览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
}

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
}
Share Improve this question edited Jan 24, 2018 at 6:34 mo_maat asked Jan 24, 2018 at 6:11 mo_maatmo_maat 2,25014 gold badges46 silver badges84 bronze badges 6
  • 3 data[0] is undefined. What are you trying to do? – mehulmpt 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. – T.J. Crowder 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 } – mo_maat 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. – mo_maat 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. – T.J. Crowder Commented Jan 24, 2018 at 6:59
 |  Show 1 more ment

2 Answers 2

Reset to default 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
}

I have an object that is evaluating as undefined

because data[0] is undefined

Directly use

if ( Object.keys( data ).length == 0 )
{
   do something
}
发布评论

评论列表(0)

  1. 暂无评论