Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 判断json是否为空

作者:岁月凉茶   发布日期:2025-12-11   浏览:61

// 判断 JSON 是否为空的示例代码

function isEmptyJSON(json) {
    // 首先检查传入的参数是否为对象类型(排除 null)
    if (typeof json !== 'object' || json === null) {
        return false;
    }

    // 如果是数组且长度为 0,则认为是空
    if (Array.isArray(json) && json.length === 0) {
        return true;
    }

    // 如果是对象且没有可枚举属性,则认为是空
    return Object.keys(json).length === 0;
}

// 示例用法
const emptyJSON = {};
const nonEmptyJSON = { key: "value" };
const jsonArray = [];
const nonEmptyJSONArray = [1, 2, 3];

console.log(isEmptyJSON(emptyJSON)); // 输出: true
console.log(isEmptyJSON(nonEmptyJSON)); // 输出: false
console.log(isEmptyJSON(jsonArray)); // 输出: true
console.log(isEmptyJSON(nonEmptyJSONArray)); // 输出: false

上一篇:js 判断字符串是否为空

下一篇:js 判断是否为空对象

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站