按值对JSON排序 [英] Sort JSON by value

查看:62
本文介绍了按值对JSON排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何按名称->值对json文件排序?我想按标签名称的asc值显示.知道如何转换我的json文件吗?

how I can sort my json file by name -> value? I want to show by asc value of tag name. Any idea how to convert my json file?

这是我的JSON文件的外观:

Here is how look my JSON file:

[{
    "id": "105",
    "name": "FIAT",
    "active": true,
    "parentId": "1"
}, {
    "id": "106",
    "name": "AUDI",
    "active": true,
    "parentId": "1"
}, {
    "id": "107",
    "name": "BMW",
    "active": true,
    "parentId": "1"
}, {
    "id": "109",
    "name": "RENAULT",
    "active": true,
    "parentId": "1"
}]

如果有人帮助我,我将不胜感激.预先感谢.

I will be grateful if someone help me. Thanks in advance.

推荐答案

我建议使用

I suggest to use Array#sort()

var data = [{ "id": "105", "name": "FIAT", "active": true, "parentId": "1" }, { "id": "106", "name": "AUDI", "active": true, "parentId": "1" }, { "id": "107", "name": "BMW", "active": true, "parentId": "1" }, { "id": "109", "name": "RENAULT", "active": true, "parentId": "1" }];
data.sort(function (a, b) {
    return a.name.localeCompare(b.name);
});
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

这篇关于按值对JSON排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆