Posted in Javascript onJanuary 14, 2019
本文实例讲述了JS实现根据数组对象的某一属性排序操作。分享给大家供大家参考,具体如下:
根据数组中对象的某一属性排序
var newArray = [{ name: "aaa", value: 0 }, { name: "ddd", value: 3 }, { name: "bbb", value: 1 }, { name: "eee", value: 4 }, { name: "ccc", value: 2 }]; function compare(property) { return function (a, b) { var value1 = a[property]; var value2 = b[property]; return value1 - value2; } } console.log(newArray.sort(compare("value")))
使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码,可得如下运行效果:
JS实现根据数组对象的某一属性排序操作示例
- Author -
The_road_of_ordinary声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@