Open sunping0612 opened 1 year ago
function sortFn(obj) {
return _.reduce(_.keys(obj).sort(), (result, key) => {
result[key] = obj[key];
return result;
}, {})
}
function sortJsonDeep(obj, orderKeys) {
const omitRoot = [];
const fullOrderKeys = _.reduce(orderKeys, (list, key) => {
const paths = key.split('.');
if (paths.length === 1) {
omitRoot.push(key);
}
if (_.findLast(paths) === '*') {
const basePath = _.slice(paths, 0, paths.length - 1);
const childKeys = _.map(
_.keys(_.get(obj, basePath)),
key => [...basePath, key].join('.'),
);
list.push(...childKeys);
} else {
list.push(key);
}
return list;
}, []);
const sortedObj = _.omit(obj, omitRoot);
_.forEach(fullOrderKeys, (key) => {
_.set(sortedObj, key, sortFn(_.get(obj, key)))
});
return sortedObj;
}
123.md