博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json数组排序
阅读量:4315 次
发布时间:2019-06-06

本文共 846 字,大约阅读时间需要 2 分钟。

/*对象*/    function JsonSort(obj, field, sortby) {        this.obj = obj;        this.field = field;        this.sortby = sortby;    }    JsonSort.prototype.sort= function() {        var $this=this;        var ascend = function(a, b) {            return a[$this.field] > b[$this.field] ? 1 : -1;        };        var descend = function(a, b) {            return a[$this.field] > b[$this.field] ? -1 : 1;        };        if (this.sortby == "ascend") {            this.obj.sort(ascend);        } else {            this.obj.sort(descend);        }    };    var json = [{        name: 'kavan',        age: 25    }, {        name: 'elle',        age: 24    }, {        name: 'jek',        age: 22    }];    var jsonSort=new JsonSort(json,'age','ascend');    jsonSort.sort();    console.log(json);

 

转载于:https://www.cnblogs.com/yexiangwang/p/5563714.html

你可能感兴趣的文章
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>
解决vmware与主机无法连通的问题
查看>>
做好产品
查看>>
项目管理经验
查看>>
笔记:Hadoop权威指南 第8章 MapReduce 的特性
查看>>
JMeter响应数据出现乱码的处理-三种解决方式
查看>>
获取设备实际宽度
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>