Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: how to check empty object in an array? [duplicate]

I am getting an array of objects from my JSON response. Sometimes I am getting an array of length 1 with an empty object. How do I check that condition?

I tried with a few things-

  myarray[0]=='empty' || myarray[0] == 'undefined'
  or myarray.indexOf(0)== -1

But didn't solved the problem

like image 468
JOGO Avatar asked Nov 29 '22 23:11

JOGO


1 Answers

You can use Object.keys() method to return all property names and check it's length:

Object.keys(myarray[0]).length === 0;
like image 68
madox2 Avatar answered Dec 01 '22 13:12

madox2