在 javascript 中对嵌套的 json 数组对象进行排序

发布于 2024-12-01 21:33:48 字数 1067 浏览 0 评论 0原文

var dataSource = ({
"Items": ({
    "Deserts": ({}),
    "Veg": ({
        "VegPulao": "Veg Pulao",
        "PalakPaneer": "Palak Paneer",
        "PaneerButterMasala": "Paneer Butter Masala"
    }),

    "Chicken": ({
        "Tandoori": "Tandoori special"
    }),
    "Hot drinks": ({
        "Coffe": ({ "Hot": "Hot Coffe", "Medium": "Medium", "Others": ({ "Iris": "Iris Coffe", "Capuccino": "Capuccino" }) }),
        "Tea": ({ "Red": "Red Tea", "Black": "Black Tea" }),
        "Hot BadamMilk": "Hot Badam Milk",
        "Hot Bornvita": "Hot Bornvita",
        "Hot Milk": "Hot Milk"
    }),
    "Juice": ({
        "Mango": "Mango",
        "Berry": "Berry",
        "Grapes": "Grapes",
        "Wine": ({
            "Rose": "Rose",
            "Red wine": "Red",
            "Apple": "Apple",
            "Hard drinks": ({
                "Royal challenge": "Royal challenge",
                "Blender's Pride": "Blender's Pride"
            })
        })
    })
})

});

需要对嵌套的 json 对象进行排序 像上面那个一样吗?

var dataSource = ({
"Items": ({
    "Deserts": ({}),
    "Veg": ({
        "VegPulao": "Veg Pulao",
        "PalakPaneer": "Palak Paneer",
        "PaneerButterMasala": "Paneer Butter Masala"
    }),

    "Chicken": ({
        "Tandoori": "Tandoori special"
    }),
    "Hot drinks": ({
        "Coffe": ({ "Hot": "Hot Coffe", "Medium": "Medium", "Others": ({ "Iris": "Iris Coffe", "Capuccino": "Capuccino" }) }),
        "Tea": ({ "Red": "Red Tea", "Black": "Black Tea" }),
        "Hot BadamMilk": "Hot Badam Milk",
        "Hot Bornvita": "Hot Bornvita",
        "Hot Milk": "Hot Milk"
    }),
    "Juice": ({
        "Mango": "Mango",
        "Berry": "Berry",
        "Grapes": "Grapes",
        "Wine": ({
            "Rose": "Rose",
            "Red wine": "Red",
            "Apple": "Apple",
            "Hard drinks": ({
                "Royal challenge": "Royal challenge",
                "Blender's Pride": "Blender's Pride"
            })
        })
    })
})

});

Need to sort a nested json object
like the one above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页 2024-12-08 21:33:48

现在你可能已经得到答案了,

function rFun(obj, newObj){
    Object.keys(obj).sort().forEach(key=>{
        if(typeof obj[key] === 'object'){
            newObj[key] = {};
            newObj[key] = rFun(obj[key], newObj[key]);
        } else {
            newObj[key] = obj[key];
        }
    });
    return newObj;
}

JSON.stringify(rFun(dataSource, {}));

它是按 ASCII 排序的,对于不区分大小写的情况,你必须编写自定义排序。

You might have got the answer by now,

function rFun(obj, newObj){
    Object.keys(obj).sort().forEach(key=>{
        if(typeof obj[key] === 'object'){
            newObj[key] = {};
            newObj[key] = rFun(obj[key], newObj[key]);
        } else {
            newObj[key] = obj[key];
        }
    });
    return newObj;
}

JSON.stringify(rFun(dataSource, {}));

it's sort by ASCII, for case insensitive you have to write custom sort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文