在Javascript中对JSON对象进行排序

编程入门 行业动态 更新时间:2024-10-10 06:22:52
本文介绍了在Javascript中对JSON对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在寻找一段时间来对这样的JSON对象进行排序

I've been looking for a while to sort a JSON object like this

{"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的值按字母顺序获得

alphabetically by value of "COMMERCIALNAME_E" to get

{"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", } ]}

我找不到任何代码将执行此操作。任何人都可以给我一些帮助吗?

I can't find any code that will do this. Can anyone give me some help?

推荐答案

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);

更多推荐

在Javascript中对JSON对象进行排序

本文发布于:2023-11-26 09:14:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   对象   Javascript   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!