Skip to content

Commit

Permalink
Create 对象多key值排序.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 authored Jan 18, 2019
1 parent b33d972 commit 0e11bd0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 对象多key值排序.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var sortObj = (obj, attr, order = 'asc') => {
var tmp = [];
var sObj = {};
sObj[attr] = {};
if (!obj[attr]) {
return;
}
for (var key in obj[attr]) {
tmp.push({
key,
value: obj[attr][key]
});
}
tmp.sort((a, b) => a.key.localeCompare(b.key));
order.toLowerCase() === 'asc' ? tmp : tmp.reverse();
tmp.map(item => sObj[attr][item.key] = item.value);
return sObj;
}
var obj = {
name: {
react: { company: 'Facebook' },
vue: { company: 'You' },
angular: { company: 'Google' },
}
};
var t1 = sortObj(obj, 'name', 'desc');
console.log(t1)

0 comments on commit 0e11bd0

Please sign in to comment.