You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Payload中移除空数组与空对象?求通用处理函数

移除空数组键与空对象的通用函数实现

我需要一个通用函数,能处理任意类型的Payload,移除所有值为空数组的键,同时移除所有空对象。以下是具体示例:

示例输入Payload

{
    "Type": "User",
    "id": "12345",
    "meta": {
        "userInfo": [],
        "lastUpdated": "2019-05-28T19:29:36"
    },
    "Relatioship": [{
        "type": {
            "coding": [{
                "system": "xyz"
            }]
        },
        "system": 99004
    }],
    "period": {
        "start": "2010-10-12T00:00:00.000Z",
        "end": "2010-10-12T00:00:00.000Z"
    },
    "Contacts": [],
    "Manager": [{
        "type": {
            "coding": []
        }
    }],
    "team": [
        []
    ],
    "account": [
        []
    ]
}

期望输出

{
    "Type": "User",
    "id": "12345",
    "meta": {
        "lastUpdated": "2019-05-28T19:29:36"
    },
    "Relatioship": [{
        "type": {
            "coding": [{
                "system": "xyz"
            }]
        },
        "system": 99004
    }],
    "period": {
        "start": "2010-10-12T00:00:00.000Z",
        "end": "2010-10-12T00:00:00.000Z"
    }
}

通用解决方案(JavaScript)

可以通过递归遍历的方式实现这个需求,函数会处理所有嵌套的对象和数组,自动清理空数组键与空对象:

function cleanPayload(obj) {
    // 处理数组:递归清理每个元素,过滤空数组和无效元素
    if (Array.isArray(obj)) {
        const cleanedArr = obj
            .map(item => cleanPayload(item))
            .filter(item => {
                if (Array.isArray(item)) return item.length > 0;
                if (typeof item === 'object' && item !== null) return Object.keys(item).length > 0;
                return item !== undefined && item !== null;
            });
        return cleanedArr.length > 0 ? cleanedArr : undefined;
    }

    // 处理对象:递归清理属性值,移除空数组/空对象对应的键
    if (typeof obj === 'object' && obj !== null) {
        const cleanedObj = {};
        for (const key in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, key)) {
                const cleanedVal = cleanPayload(obj[key]);
                // 仅保留有效非空值
                if (cleanedVal !== undefined) {
                    if (Array.isArray(cleanedVal) && cleanedVal.length === 0) continue;
                    if (typeof cleanedVal === 'object' && Object.keys(cleanedVal).length === 0) continue;
                    cleanedObj[key] = cleanedVal;
                }
            }
        }
        return Object.keys(cleanedObj).length > 0 ? cleanedObj : undefined;
    }

    // 基本类型直接返回
    return obj;
}

函数逻辑说明

  1. 数组处理:遍历数组每个元素并递归清理,过滤掉处理后为空的数组、空对象,以及undefined/null值,最终只保留非空数组。
  2. 对象处理:遍历对象所有属性,递归清理属性值后,移除值为空数组或空对象的键;如果处理后的对象为空,则返回undefined,会被上层逻辑过滤。
  3. 基本类型:直接返回,不做处理。

使用示例

将示例输入传入函数,即可得到期望输出:

const inputPayload = { /* 示例输入内容 */ };
const cleanedPayload = cleanPayload(inputPayload);
console.log(JSON.stringify(cleanedPayload, null, 2));

内容的提问来源于stack exchange,提问作者Hasan Limon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:13:11