首页 > 代码库 > 数组对象用ID排序
数组对象用ID排序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
11111
</body>
</html>
<script>
var arr = [
{sizeId:"001",sizeName:"M"},
{sizeId:"002",sizeName:"L"},
{sizeId:"004",sizeName:"S"},
{sizeId:"008",sizeName:"SM"},
{sizeId:"006",sizeName:"SX"}
];
function compare(prop) {
return function (obj1,obj2) {
var val1 = obj1[prop];
var val2 = obj2[prop];
return val1 -val2;
}
}
var sortObj = arr.sort(compare("sizeId"));
console.log(sortObj);
</script>
数组对象用ID排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。