对json数组进行排序和filter过滤

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>对json数组进行排序和filter过滤</title>
</head>
<body>
<script>



var data = [
	{recordId: 10, recordNo: "PPCZ_", userId: 1, recordType: 9, recordCode: "", recordInMoney: 1111},
	{recordId: 5, recordNo: "PPCZ_20170105170203", userId: 1, recordType: 9,recordInMoney: 11},
	{recordId: 2, recordNo: "PPCZ_20170105170201", userId: 1, recordType: 9,recordInMoney:500}
]


/*
 * @description		根据某个字段实现对json数组的排序
 * @param	 array	要排序的json数组对象
 * @param	 field	排序字段(此参数必须为字符串)
 * @param	 reverse  是否倒序(默认为false)
 * @return	array	返回排序后的json数组
*/
function jsonSort(array, field, reverse) {
	//数组长度小于2 或 没有指定排序字段 或 不是json格式数据
	if(array.length < 2 || !field || typeof array[0] !== "object") return array;
	//数字类型排序
	if(typeof array[0][field] === "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;
}

            //使用方法 
console.log(jsonSort(data,'recordId'));

//filter方法
function gtFilter(value) {
   return value.recordId > 5
}

console.log(data.filter(gtFilter))



</script>
	
</body>
</html>

猜你喜欢

转载自blog.csdn.net/kongjiea/article/details/54616475