json数组本地获取,以及根据字段排序【部分摘录】

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json+ajax</title>
<script src="jquery-1.11.1.min.js"></script>
<script src="data.json"></script>

<!-- var data=[

{"id":"4","name":"text4","count":"1"},
{"id":"4","name":"text5","count":"7"},
{"id":"4","name":"text6","count":"3"}
] -->
</head>
<body>
<script>
// console.log(data);
function jsonSort(array,field,reverse){
if(array.length<2||!field||typeof array[0]!=="object") return array;
if(typeof array[0]==="number"){
array.sort(function(x,y){return x[field]-y[field]});
}
if(typeof array[0][field]==="string"){
array.sort(function(x,y){return x[field].localeCompare(y[field])});
}
if(reverse){
array.reverse();
}
return array;
}
var data=jsonSort(data,"count",false);
for(var i=0;i<data.length;i++){
$.each(data[i],function(index,value){
console.log(data[i].name);
})
}
</script>
</body>
</html>

上一篇:VFS四大对象之四-struct file


下一篇:Intersect交集Except差集Union并集实例