jQuery 检查 JSON 数组是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18396073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a JSON array is empty
提问by Thanos
I know from the first look it sounds like duplicate question but i don't think it is...
我从第一眼就知道这听起来像是重复的问题,但我不认为它是......
I am receiving back a JSON array as:
我收到一个 JSON 数组:
var test1 = [] ;
or
或者
var test2 = [{},{},{}] ; //This is empty
I have no problem finding out if test1
is empty.
我没有问题找出是否test1
为空。
jQuery.isEmptyObject(test1)
My problem is with the test2
...
Please note that in some cases the test2 might return something like:
我的问题是test2
......请注意,在某些情况下,test2 可能会返回如下内容:
var test2 = [{"a":1},{},{}] ; //All these are not empty
var test2 = [{},{"a":1},{}] ; //All these are not empty
var test2 = [{},{},{"a":1}] ; //All these are not empty
The above scenarios shouldn't be counted as empty.I've tried to use .length
but it's not helping as the length is always 3... Any ideas?
上面的场景不应该算作空。我试过使用,.length
但它没有帮助,因为长度总是 3...有什么想法吗?
Cheers.
干杯。
采纳答案by c.P.u1
function isArrayEmpty(array) {
return array.filter(function(el) {
return !jQuery.isEmptyObject(el);
}).length === 0;
}
Passes all of your tests.
通过您的所有测试。
A pure JavaScript solution would be to replace !jQuery.isEmptyObject(el)
with Object.keys(el).length !== 0
纯 JavaScript 解决方案是替换!jQuery.isEmptyObject(el)
为Object.keys(el).length !== 0
Edit: Using Array.prototype.every
function isArrayEmpty(array) {
return array.every(function(el) {
return jQuery.isEmptyObject(el);
});
}
回答by Evan Trimboli
For those playing at home, a non jQuery solution:
对于那些在家玩的人,一个非 jQuery 解决方案:
var test2 = [{a: 1},{},{}] ; //This is empty
function isEmpty(val) {
var len = val.length,
i;
if (len > 0) {
for (i = 0; i < len; ++i) {
if (!emptyObject(val[i])) {
return false;
}
}
}
return true;
}
function emptyObject(o) {
for (var key in o) {
if (o.hasOwnProperty(key)) {
return false;
}
}
return true;
}
console.log(isEmpty(test2));
回答by kunde
I had the same problem, and I come with this solution without jQuery:
我遇到了同样的问题,我提供了没有 jQuery 的解决方案:
function isEmpty(x) {
for(var i in x) {
return false;
}
return true;
}
回答by KooiInc
Without JQuery: using Array.filter
1and Object.keys
2:
没有 JQuery:使用Array.filter
1和Object.keys
2:
function JSONEmpty(obj){
return !obj.length ||
!obj.filter(function(a){return Object.keys(a).length;}).length;
}
// usage
JSONEmpty([{"a":1},{},{}]); //=> false
JSONEmpty([{},{"a":1},{}]); //=> false
JSONEmpty([{},{},{"a":1}]); //=> false
JSONEmpty([]); //=> true
JSONEmpty([{},{},{}]); //=> true
update 2018Arrow functionsare now supported by all modern browsers, so like himel-nag-ranastipulated, you can also use:
update 2018 Arrow 功能现在所有现代浏览器都支持,所以像himel-nag-rana规定的那样,你也可以使用:
const JSONEmpty = obj => !obj.length || !obj.filter(a => Object.keys(a).length).length;
1More info
2More info(links contain shims for older browsers)
回答by Eadel
Maybe you could try use function like
也许你可以尝试使用类似的功能
function isEmptyObject (test) {
for (var i in test) {
if (!jQuery.isEmptyObject(test[i])
return false;
}
return true;
}
回答by tarikki
Here's my take: turn the array into a set and check for size.
这是我的看法:将数组变成一个集合并检查大小。
var myArray = [1,2,3];
var mySet = new Set(myArray);
console.log(mySet.size === 0);
回答by Ch33f
Pretty simple...
很简单...
if(jQuery.isEmptyObject(test2[0]) && jQuery.isEmptyObject(test2[1]) && jQuery.isEmptyObject(test2[2])))
// do something
回答by Arun Bertil
check by looping each values in array and return error Try
通过循环数组中的每个值进行检查并返回错误尝试
for(i=0;js_array[i]!=null;i++)
{
if(js_array[i]=="")
{
alert("Empty");
}
}