With JavaScript, it can be difficult to check whether an object is empty. With Arrays, you can easily check withmyArray.length, but on the other hand, objects do not work that way. The best way to check if an object is empty is by using a utility function like the one below. functi...
避免js内置构造函数也返回true 2. 通过原型判断(当浏览器不支持es6时使用) functionisEmptyObject(obj){if(Object.prototype.toString.call(obj)==='[object Object]'&&JSON.stringify(obj)==='{}'){returntrue}returnfalse} 3.通过for...in 和 hasOwnProperty functionisEmptyObject(obj){for(letkeyinobj){...
returntrue// 如果不为空,则会执行到这一步,返回true 方法四:jquery的isEmptyObject方法 此方法是jquery将2方法(for in)进行封装,使用时需要依赖jquery vardata = {}; varb = $.isEmptyObject(data); alert
How to Check if Object is Empty in JavaScriptHere's a Code Recipe to check if an object is empty or not. For newer browsers, you can use plain vanilla JS and use the new "Object.keys" 🍦 But for older browser support, you can install the Lodash library and use their "isEmpty" ...
2 jQuery.isEmptyObject({"foo":"1"}); // false As you can see, it’s fairly straightforward to use the isEmptyObject method with jQuery. Similarly, the Lodash and Underscore.js libraries have _.isEmpty(). Conclusion In this article, we discussed a number of different ways to check ...
<script>functionObjIsEmpty(obj){varisEmpty =true//如果存在任何一个属性,则将isEmpty赋值为falsefor(keyinobj){ isEmpty =false}returnisEmpty }varobj = {}varobj2 = {a:1,b:2}console.log(ObjIsEmpty(obj))//trueconsole.log(ObjIsEmpty(obj2))//false</script> ...
Hoek.deepEqual({}, emptyObject);// true Conclusion In this article, we have taken a look at how to check whether an object is empty or not in JavaScript. To this end - we've used several Vanilla JS approaches, jQuery, Underscore, Lodash, Ramda, Hoek and the JSON module....
js判断空对象的方法 判断一个js对象是否是空对象isEmptyObject author: @TiffanysBear 方法一:...
判断的对象不一样,$.isEmptyObject判断的是js对象即{},你理解的是jquery的DOM对象。 有用1 回复 指间流过的沙: 对象里面的数组也是可以用($.isEmptyObject)的对吧,比如:{[...]} 回复2016-11-25 Zlatan: 不可以的,你可以简单试一下嘛。 回复2016-11-25 不爱吃...
You should also make sure the object is actually an object, by checking its constructor is the Object object:objectToCheck.constructor === ObjectLodash, a popular library, makes it simpler by providing the isEmpty() function:_.isEmpty(objectToCheck)...