如何在JavaScript中使用主节点文本对json对象进行排序
How to sort the json object with main node text in JavaScript?
我有json对象,希望根据variablevalue对整个json对象进行排序。
Json对象:
{
"Surveyors, Cartographers, and Photogrammetrists":[
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":53401.018
},
"variablename":"Average
annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000.0
},
"variablename":"Median annual wage
($)"
},
{
"variablevalue":{
"color":"Blue",
"y":93.7004
},
"variablename":"Offshorability rank (0-100)"
},
{
"variablevalue":{
"color":"Black",
"y":77.76633
},
"variablename":"Male (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":70.81703333333334
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":55.7
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"Blue",
"y":34.8
},
"variablename":"Labor Shortage Index (Flexibility)"
},
{
"variablevalue":{
"color":"Black",
"y":23.87058222069003
},
"variablename":"Projected Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":23.456978920690027
},
"variablename":"Projected New Entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":18.2
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"Blue",
"y":15.606653247064578
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (%
of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":14.623767403
},
"variablename":"Average years
of schooling (years)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":14.0
},
"variablename":"Typical years
of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":12.93433
},
"variablename":"Immigrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":12.622308824363992
},
"variablename":"Projected
Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":11.47879
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":10.80817
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkRed",
"y":8.5
},
"variablename":"Labor Shortage Index
(Demand-Supply)"
},
{
"variablevalue":{
"color":"Blue",
"y":5.29082
},
"variablename":"Teleworkers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":2.0697345889048466
},
"variablename":"Unionized workers
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":1.98172
},
"variablename":"Part-time
workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":1.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.4136033
},
"variablename":"Projected
Re-entrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":0.0
},
"variablename":"Typical
work experience (0-2)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.0
},
"variablename":"Typical training
(0-6)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":-2.984344422700591
},
"variablename":"Projected Employment
Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":-11.248273396326036
},
"variablename":"Projected
Demand-Supply gap (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":-16.71060203279864
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":443900.0
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":443000.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":56048.869
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000 .0
},
"variablename":"Median annual wage ($)"
},
{
"variablevalue":{
"color":"Blue",
"y":80.75036
},
"variablename":"Male (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":70.81703333333334
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":63.3
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":59.199999999999996
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"Blue",
"y":57.199999999999996
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"Black",
"y":55.2
},
"variablename":"Labor
Shortage Index (Flexibility)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":54.9048
},
"variablename":"Offshorability
rank (0-100)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":26.00451467268623
},
"variablename":"Projected
Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":25.80135440180587
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":18.895277715376846
},
"variablename":"Projected Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":18.481674415376844
},
"variablename":"Projected New Entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":15.282309999999999
},
"variablename":"Immigrants (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":14.464026496923166
},
"variablename":"Unionized workers
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":14.094928248999999
},
"variablename":"Average
years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":14.0
},
"variablename":"Typical
years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":11.00492
},
"variablename":"Skill
measure*"
},
{
"variablevalue":{
"color":"Blue",
"y":7.58775
},
"variablename":"Part-time workers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":7.109236957309385
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":3.35128
},
"variablename":"Part-time
workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":2.049
},
"variablename":"Teleworkers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":1.0
},
"variablename":"Certification
measure (0-2)"
},
{
"variablevalue":{
"color":"Black",
"y":0.4136033
},
"variablename":"Projected Re-entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.2031602708803648
},
"variablename":"Projected Employment Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":0.0
},
"variablename":"Typical work experience (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.0
},
"variablename":"Typical
training (0-6)"
},
{
"variablevalue":{
"color":"Black",
"y":-1.9212370833226702
},
"variablename":"Projected
Demand-Supply gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":57299.999237
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":52900.001526
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":50277.343
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":45000.0
},
"variablename":"Median annual
wage ($)"
},
{
"variablevalue":{
"color":"Blue",
"y":96.001
},
"variablename":"Probability of computerisation
(%)"
},
{
"variablevalue":{
"color":"Black",
"y":91.34864999999999
},
"variablename":"Male (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":61.4
},
"variablename":"Labor Shortage Index (Flexibility
)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50.0
},
"variablename":"Labor Shortage Index (Education & Skills
)"
},
{
"variablevalue":{
"color":"Blue",
"y":20.375618388959438
},
"variablename":"Projected Labor Supply
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":20.3
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":19.962015088959436
},
"variablename":"Projected New Entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":19.02268386761445
},
"variablename":"Projected Replacement of Jobs, 2014-2024 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":13.730615929999999
},
"variablename":"Average years of schooling (years)"
},
{
"variablevalue":{
"color":"Black",
"y":12.5
},
"variablename":"Offshorability rank (0-100)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":12.0
},
"variablename":"Typical years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":11.343804688574572
},
"variablename":"Projected Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":11.27454
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":10.76173
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":10.299999999999999
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":5.6590300000000004
},
"variablename":"Immigrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":4.123637228124855
},
"variablename":"Unionized workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":3.96216
},
"variablename":"Teleworkers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":3.0
},
"variablename":"Typical training (0-6)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":2.1993300000000002
},
"variablename":"Part-time workers younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.4136033
},
"variablename":"Projected Re-entrants (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Black",
"y":0.0
},
"variablename":"Typical work experience (0-2)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":0.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":-7.6788791790398765
},
"variablename":"Projected Employment Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":-9.031813700384866
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Black",
"y":-15.689753054049923
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment, adjusted)"
},
{
"variablevalue":{
"color":"Blue",
"y":59299.999237
},
"variablename":"Projected Employment, 2024 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":58259.97
},
"variablename":"Average annual wage ($)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":56599.998474
},
"variablename":"Employment
, 2014 (persons)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":50000.0
},
"variablename":"Median annual wage
($)"
},
{
"variablevalue":{
"color":"Blue",
"y":90.10000000000001
},
"variablename":"Labor Shortage Index
(Flexibility)"
},
{
"variablevalue":{
"color":"Black",
"y":82.74463
},
"variablename":"Male (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":79.3
},
"variablename":"Labor Shortage Index"
},
{
"variablevalue":{
"color":"DarkRed",
"y":72.8
},
"variablename":"Labor Shortage Index (Education & Skills)"
},
{
"variablevalue":{
"color":"Blue",
"y":59.8
},
"variablename":"Labor Shortage Index (Demand-Supply)"
},
{
"variablevalue":{
"color":"Black",
"y":37.45583274660072
},
"variablename":"Projected Labor Demand (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":32.685513248729556
},
"variablename":"Projected Replacement of Jobs, 2014-2024
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":27.859418869633
},
"variablename":"Projected
Labor Supply (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Blue",
"y":26.516515869633
},
"variablename":"Projected New Entrants (% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":19.9168
},
"variablename":"Probability of computerisation (%)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":16.048081623
},
"variablename":"Average years of schooling (years)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":16.0
},
"variablename":"Typical
years of schooling (years)"
},
{
"variablevalue":{
"color":"Blue",
"y":12.5
},
"variablename":"Offshorability
rank (0-100)"
},
{
"variablevalue":{
"color":"Black",
"y":10.97879
},
"variablename":"Skill measure*"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":9.59849
},
"variablename":"Part-time workers (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":9.59641387696772
},
"variablename":"Projected Demand-Supply gap (% of 2014 employment
)"
},
{
"variablevalue":{
"color":"Blue",
"y":6.93850826190742
},
"variablename":"Unionized workers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"Black",
"y":5.74764
},
"variablename":"Teleworkers (% of 2014
employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":5.03074
},
"variablename":"Immigrants (% of 2014
employment)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":4.770319497871167
},
"variablename":"Projected Employment
Growth, 2014-2024 (%)"
},
{
"variablevalue":{
"color":"Blue",
"y":1.342903
},
"variablename":"Projected Re-entrants
(% of 2014 employment)"
},
{
"variablevalue":{
"color":"Black",
"y":1.30934
},
"variablename":"Part-time workers
younger than 24 (% of 2014 employment)"
},
{
"variablevalue":{
"color":"DarkOrange",
"y":1.0
},
"variablename":"Certification measure (0-2)"
},
{
"variablevalue":{
"color":"DarkRed",
"y":0.782686
},
"variablename":"Typical
work experience (0-2)"
},
{
"variablevalue":{
"color":"Blue",
"y":0.0
},
"variablename":"Typical training
(0-6)"
},
{
"variablevalue":{
"color":"Black",
"y":-1.8435157600876302
},
"variablename":"Projected Demand-Supply
gap (% of 2014 employment, adjusted)"
}
]
}
我试过功能
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
});
}
但它只返回内部json对象,并错过了主节点文本,如:
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
}
所以排序后的最终结果是:
"Surveyors, Cartographers, and Photogrammetrists":[
{
"variablevalue":{
"color":"Blue",
"y":204400.0
},
"variablename":"Employment, 2014 (persons)"
},
{
"variablevalue":{
"color":"Black",
"y":198300.0
},
"variablename":"Projected
Employment, 2024 (persons)"
}
}
请帮帮我。
假设要按variablevalue.y
值降序排序,而它从来不是null
或undefined
。
尝试
obj["Surveyors, Cartographers, and Photogrammetrists"].sort(function(a,b){
return parseFloat(b.variablevalue.y) - parseFloat(a.variablevalue.y);
});
如果variablevalue.y
可以是null
、undefined
或NaN
,则
obj["Surveyors, Cartographers, and Photogrammetrists"].sort(function(a,b){
var by = !isNaN(parseFloat(b.variablevalue.y)) || 0;
var ay = !isNaN(parseFloat(a.variablevalue.y)) || 0;
return by - ay;
});
相关文章:
- 如何使用Windows8JSON.parse将JSON文本反序列化为日期类型
- 在freemarker中显示JSON文本
- 为什么从JSON文本到类型的转换不包括函数
- 如何为 After Effects 设置文件夹以监视 JSON/文本文件
- JSON 处理 - 向 JSP 发送 JSON 文本字符串,如何在 JSP 中处理
- JQuery 不加载 JSON 文本
- 在 JavaScript 中将字符串转换为 JSON 文本
- 将代码鸟回复解析为数组(或将 json 文本解析为代码鸟的回复)
- 想要上传和解析 json 文本,并通过使用 ajax 单击按钮将其加载到我的索引页
- 如何使用咕噜声缩小 JSON 文本文件
- 如何使用 Jquery 在段落中以纯文本形式显示我的数据,该段落以 JSON 文本包装
- 我的表上的jquery表排序器出现问题,我收到一堵json文本墙
- 在JavaScript中使用和存储json文本
- 将php多维数组中编码的json文本转换为fullcalendar的json对象
- 使用JQuery将未格式化的JSON文本添加到ACE Editor Div
- 从Javascript中的JSON文本中提取值
- 从HTML iframe中获取JSON文本
- 仅使用JavaScript/jQuery在网页中下载JSON文本文件
- 如何使用javascript访问html元素并将其存储在json文本中
- 如何使用索引检索JSON文本