is Object empty

https://leetcode.com/problems/is-object-empty/description/

Toh yha par humko object ka empty hona check karna hai 

But 
typeof  null---> "object"

type of [] --->"object"

type of {} ---> object 

console.log(typeof  null)

console.log(typeof [] )

console.log(typeof {}  )




toh what we can do--> sabse phley Array.isArray se Array ka pata lagaya ja sakta hai
fir agar wo array nahi hai toh ya toh null ya fir {} ho sakta hai

fir Object.keys(obj) se uski length check karlo agr wo object hai aur null nahi hai

       

 var isEmpty = function(obj) {
  if (Array.isArray(obj)) {
    return obj.length === 0;
  } else if (typeof obj === 'object' && obj !== null) {
    return Object.keys(obj).length === 0;
  }
  return false; 
};

       
 

Comments

Popular posts from this blog

TO the new

4048