最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

is this object empty, Javascript - Stack Overflow

matteradmin12PV0评论

I have an ajax system set up. When the MySQL query returns no data, I need it to pass an empty object back. I create a node called 'data' in the php script and even when the query returns no data I pass $data['success'] = 1.

The trick is I can't figure out how to check to see if the query was returned data or not.

I have tried...

// sub responseObj.data for responseObj.data[0] for the following if's
if(responseObj.data[0].length == -1)  

if(responseObj.data[0] == null)

if(responseObj == undefined)
//edit: added this...
if(!responseObj.data[0])

and I've really lost tack of any other various snippet's i've tried.

EDIT: adding xml generated that is passed to my script
XML - returning zero results

<response_myCallbackFunction>  
  <success>1</success>  
<response_myCallbackFunction>

XML - returning a populated query

<response_myCallbackFunction>  
  <data> 
  <random_data>this is data</random_data>  
  </data>  
  <success>1</success>  
<response_myCallbackFunction>

Is there a way to check to see if an object is empty in javascript?

-thanks

I have an ajax system set up. When the MySQL query returns no data, I need it to pass an empty object back. I create a node called 'data' in the php script and even when the query returns no data I pass $data['success'] = 1.

The trick is I can't figure out how to check to see if the query was returned data or not.

I have tried...

// sub responseObj.data for responseObj.data[0] for the following if's
if(responseObj.data[0].length == -1)  

if(responseObj.data[0] == null)

if(responseObj == undefined)
//edit: added this...
if(!responseObj.data[0])

and I've really lost tack of any other various snippet's i've tried.

EDIT: adding xml generated that is passed to my script
XML - returning zero results

<response_myCallbackFunction>  
  <success>1</success>  
<response_myCallbackFunction>

XML - returning a populated query

<response_myCallbackFunction>  
  <data> 
  <random_data>this is data</random_data>  
  </data>  
  <success>1</success>  
<response_myCallbackFunction>

Is there a way to check to see if an object is empty in javascript?

-thanks

Share Improve this question edited Dec 8, 2010 at 18:36 Derek Adair asked Dec 22, 2009 at 15:58 Derek AdairDerek Adair 21.9k31 gold badges101 silver badges134 bronze badges 2
  • 3 can you add a snippet from your php for what you are returning – robjmills Commented Dec 22, 2009 at 16:02
  • possible duplicate of How do I test for an empty Javascript object from JSON? – Derek Adair Commented Nov 6, 2013 at 16:46
Add a ment  | 

5 Answers 5

Reset to default 7

Obj.hasOwnProperty('blah') does not seem to work for checking to see if the property exists.

function isEmptyObj(obj){
  for(var i in obj){
    return false;
  }
  return true;
}

isEmptyObj({a:1}); //returns true

isEmptyObj({}); //returns false

You could try

if( responseObj["data"] ) {
   // do stuff with data
}

or

if( responseObj.hasOwnProperty("data") && responseObj.data ) {
   // do stuff with data
}
if(typeof responseObj.data != 'undefined') {
   // code goes here
}

for ES5 you have getOwnPropertyNames :

var o = { a:1, b:2, c:3 };
Object.getOwnPropertyNames(o).length // 3

If responseObj is the XML Document object (from the xhr.responseXML property), then:

if (responseObj.getElementsByTagName("data").length > 0) {
    // do stuff...
}

If responseObj is a JavaScript object:

if (responseObj.data) {
    // do stuff...
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far