为了账号安全,请及时绑定邮箱和手机立即绑定

用Javascript排序JSON对象

用Javascript排序JSON对象

胡说叔叔 2019-12-13 15:10:06
我一直在寻找排序这样的JSON对象的时间{"results": [  {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "35",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "SADD MAARAB PHARMACY",      },    "geometryType": "esriGeometryPoint",   },  {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "1",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "GAYATHY HOSPITAL  PHARMACY",    },    "geometryType": "esriGeometryPoint",  },     {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "255",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "AL DEWAN PHARMACY",      },    "geometryType": "esriGeometryPoint",   }]}按“ COMMERCIALNAME_E”的字母顺序获取{"results": [   {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "255",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "AL DEWAN PHARMACY",      },    "geometryType": "esriGeometryPoint",   },  {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "1",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "GAYATHY HOSPITAL  PHARMACY",       },    "geometryType": "esriGeometryPoint",   },   {    "layerId": 5,    "layerName": "Pharmaceutical Entities",    "attributes": {      "OBJECTID": "35",      "FACILITYTYPE": "Pharmacy",      "FACILITYSUBTYPE": "24 Hr Pharmacy",      "COMMERCIALNAME_E": "SADD MAARAB PHARMACY",      },    "geometryType": "esriGeometryPoint",   }]}我找不到任何可以做到这一点的代码。谁能给我些帮助吗?
查看完整描述

3 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

function sortJsonArrayByProperty(objArray, prop, direction){

    if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");

    var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending


    if (objArray && objArray.constructor===Array){

        var propPath = (prop.constructor===Array) ? prop : prop.split(".");

        objArray.sort(function(a,b){

            for (var p in propPath){

                if (a[propPath[p]] && b[propPath[p]]){

                    a = a[propPath[p]];

                    b = b[propPath[p]];

                }

            }

            // convert numeric strings to integers

            a = a.match(/^\d+$/) ? +a : a;

            b = b.match(/^\d+$/) ? +b : b;

            return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );

        });

    }

}


sortJsonArrayByProperty(results, 'attributes.OBJECTID');

sortJsonArrayByProperty(results, 'attributes.OBJECTID', -1);

更新:请勿更改


function sortByProperty(objArray, prop, direction){

    if (arguments.length<2) throw new Error("ARRAY, AND OBJECT PROPERTY MINIMUM ARGUMENTS, OPTIONAL DIRECTION");

    if (!Array.isArray(objArray)) throw new Error("FIRST ARGUMENT NOT AN ARRAY");

    const clone = objArray.slice(0);

    const direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending

    const propPath = (prop.constructor===Array) ? prop : prop.split(".");

    clone.sort(function(a,b){

        for (let p in propPath){

                if (a[propPath[p]] && b[propPath[p]]){

                    a = a[propPath[p]];

                    b = b[propPath[p]];

                }

        }

        // convert numeric strings to integers

        a = a.match(/^\d+$/) ? +a : a;

        b = b.match(/^\d+$/) ? +b : b;

        return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );

    });

    return clone;

}


const resultsByObjectId = sortByProperty(results, 'attributes.OBJECTID');

const resultsByObjectIdDescending = sortByProperty(results, 'attributes.OBJECTID', -1);



查看完整回答
反对 回复 2019-12-14
?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

首先提取JSON编码的数据:


var data = eval(yourJSONString);

var results = data['results'];

然后使用自定义(用户)函数进行排序:


results.sort(function(a,b){

    //return a.attributes.OBJECTID - b.attributes.OBJECTID;

    if(a.attributes.OBJECTID == b.attributes.OBJECTID)

        return 0;

    if(a.attributes.OBJECTID < b.attributes.OBJECTID)

        return -1;

    if(a.attributes.OBJECTID > b.attributes.OBJECTID)

        return 1;

});

我假设您想按排序OBJECTID,但是您可以将其更改为按任何排序。



查看完整回答
反对 回复 2019-12-14
?
不负相思意

TA贡献1777条经验 获得超10个赞

您可以通过提供自定义比较功能作为的参数,对任何东西的有序数组进行排序Array.Sort()。


var myObject = /* json object from string */ ;


myObject.results.sort(function (a, b) {


    // a and b will be two instances of your object from your list


    // possible return values

    var a1st = -1; // negative value means left item should appear first

    var b1st =  1; // positive value means right item should appear first

    var equal = 0; // zero means objects are equal


    // compare your object's property values and determine their order

    if (b.attributes.COMMERCIALNAME_E < a.attributes.COMMERCIALNAME_E) {

        return b1st;

    }

    else if (a.attributes.COMMERCIALNAME_E < b.attributes.COMMERCIALNAME_E) {

        return a1st;

    }

    else {

        return equal;

    }

});



查看完整回答
反对 回复 2019-12-14
  • 3 回答
  • 0 关注
  • 290 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号