var array = [
{name:"小明",age:12},
{name:"小红",age:13},
{name:"小花",age:11}
]
function compare(property) {
return function (a, b) {
return a[property] - b[property];
};
}
array.sort(compare('age'));
结果为:
[
{name:"小花",age:11},
{name:"小明",age:12},
{name:"小红",age:13}
]
js实现按照数组中某个对象的属性值排序
最新推荐文章于 2024-05-24 16:55:11 发布