SOURCE

let textObj = {
    "contractNumber": null,
    "contractCreated": null,
    "buyerContractNumber": null,
    "buyer": {
        "company": "Hangzhou Fanwen Technology Co., Ltd.",
        "address": "Tower C, Huifeng International, Xihu District, Hangzhou City",
        "addressTop2": [
            "ShangHai",
            "SuZhou"
        ],
        "attn": null,
        "email": null,
        "tel": null
    },
    "order": {
        "grandTotalPrice": 0,
        "products": [
            {
                "model": "大力神",
                "description": null,
                "piece": 1,
                "price": 0,
                "totalEXW": 0,
                "taxRate": 0.17,
                "spec_Decal": null,
                "spec_Decal_Key": "Decal&Data plate",
                "spec_Color": "Black",
                "spec_Color_Key": "Color",
                "spec_LoadCapacity": null,
                "spec_LoadCapacity_Key": "Capacity",
                "spec_LiftingHeight": 20,
                "spec_LiftingHeight_Key": "Lifting height",
                "spec_ForkDimensions": null,
                "spec_ForkDimensions_Key": "Fork length",
                "spec_Battery": null,
                "spec_Battery_Key": "Battery",
                "spec_Charger": null,
                "spec_Charger_Key": "Charger",
                "spec_Carriage": null,
                "spec_Carriage_Key": "Carriage"
            },
            {
                "model": "威震天",
                "description": null,
                "piece": 1,
                "price": 0,
                "totalEXW": 0,
                "taxRate": 0.17,
                "spec_Decal": null,
                "spec_Decal_Key": "Decal&Data plate",
                "spec_Color": "Black",
                "spec_Color_Key": "Color",
                "spec_LoadCapacity": null,
                "spec_LoadCapacity_Key": "Capacity",
                "spec_LiftingHeight": null,
                "spec_LiftingHeight_Key": "Lifting height",
                "spec_ForkDimensions": null,
                "spec_ForkDimensions_Key": "Fork length",
                "spec_Battery": null,
                "spec_Battery_Key": "Battery",
                "spec_Charger": null,
                "spec_Charger_Key": "Charger",
                "spec_Carriage": null,
                "spec_Carriage_Key": "Carriage"
            }
        ]
    }
}

function formatObjectOptimized(obj) {
    const formatKeyValue = (obj, prefix = '') => {
        return Object.entries(obj)
            .filter(([key, value]) => value !== null && value !== '')
            .map(([key, value]) => {
                if (Array.isArray(value)) {
                    const filtered = value.filter(item => item !== null && item !== '');
                    return filtered.length > 0 ? `${key}:${filtered.join(', ')}` : null;
                }
                return `${key}:${value}`;
            })
            .filter(Boolean)
            .join(',');
    };

    const buyerString = formatKeyValue(obj.buyer);

    const productStrings = obj.order.products
        .filter(product => product.model)
        .map(product => {
            const normalProps = Object.entries(product)
                .filter(([key, value]) => 
                    value !== null && 
                    value !== '' && 
                    !key.startsWith('spec_')
                )
                .map(([key, value]) => `${key}:${value}`);

            const specProps = Object.entries(product)
                .filter(([key, value]) => 
                    key.startsWith('spec_') && 
                    !key.endsWith('_Key') && 
                    value !== null && 
                    value !== ''
                )
                .map(([key, value]) => {
                    const displayKey = product[`${key}_Key`] || key.replace('spec_', '');
                    return `${displayKey}:${value}`;
                });

            return [...normalProps, ...specProps].join(',');
        });
        console.log(obj.order.grandTotalPrice)
        // if(obj.order.grandTotalPrice != null){
        //     productStrings = `,grandTotalPrice:${obj.order.grandTotalPrice}`
        // }
    return {
        company: buyerString,
        products: productStrings.join(';')
    };
}

// 使用示例
const result = formatObjectOptimized(textObj);
console.log(result);
console 命令行工具 X clear

                    
>
console