[英]sort object by content type
如何按内容类型对对象进行排序?
const myObject = {
"id": 3,
"name": "my object",
"content": ["a", "b", "c"],
"description": "my description",
"anotherArray": [11, 22, 33],
"contentObject": {
"id": 56,
"name": "content object"
}
}
我希望排序顺序是:
我发现了这个, Sorting JavaScript Object by property value ,但我正在寻找按内容类型排序,而不是内容值。
到目前为止我有...
const getDefaultTypeValue = (value) => {
const val = value;
let newVal = null;
const isString = (value) => (typeof value === 'string' || value instanceof String);
const isNumber = (value) => (typeof value === 'number' && isFinite(value));
const isArray = (value) => (Array.isArray(value));
const isFunction = (value) => (typeof value === 'function');
const isNull = (value) => (value === null);
const isUndefined = (value) => (typeof value === 'undefined');
const isBoolean = (value) => (typeof value === 'boolean');
const isDate = (value) => (value instanceof Date);
const isRegExp = (value) => (value && typeof value === 'object' && value.constructor === RegExp);
const isError = (value) => (value instanceof Error && typeof value.message !== 'undefined');
const isSymbol = (value) => (typeof value === 'symbol');
if (isString(val)) {
newVal = '';
} else if (isNumber(val)) {
newVal = null;
} else if (isArray(val)) {
newVal = [];
} else if (isFunction(val)) {
newVal = val;
} else if (isUndefined(val)) {
newVal = undefined;
} else if (isBoolean(val)) {
newVal = false;
} else if (isDate(val)) {
newVal = new Date();
} else if (isRegExp(val)) {
newVal = '';
} else if (isError(val)) {
newVal = null;
} else if (isSymbol(val)) {
newVal = null;
} else if (isNull(val)) {
newVal = null;
}
return newVal;
}
但我真的想不出如何实现这一点。
预期输出:
const myObject = {
"id": 3,
"name": "my object",
"description": "my description",
"content": ["a", "b", "c"],
"anotherArray": [11, 22, 33],
"contentObject": {
"id": 56,
"name": "content object"
}
}
所以首先,对象不能这样排序; 这使得整个操作有点困难 - 但是,您可以对对象键进行排序。 要做到这一点 - 它与按字母顺序或然而排序键相同。 创建一个数组,按照您想要的顺序进行检查,并使用索引
const checks = [ isNumber, isString, isArray, isPlainSybol, isFunction, isNull, isUndefined, isBoolean, isDate, isRegExp, isError, isSymbol ]; const myObject = { "id": 3, "name": "my object", "content": ["a", "b", "c"], "description": "my description", "anotherArray": [11, 22, 33], "contentObject": { "id": 56, "name": "content object" } } const sortedKeys = Object.keys(myObject).sort((a,b) => checks.findIndex(e => e(myObject[a])) - checks.findIndex(e => e(myObject[b])) ) console.log(sortedKeys);
<script> const isString = (value) => (typeof value === 'string' || value instanceof String); const isNumber = (value) => (typeof value === 'number' && isFinite(value)); const isArray = (value) => (Array.isArray(value)); const isFunction = (value) => (typeof value === 'function'); const isNull = (value) => (value === null); const isUndefined = (value) => (typeof value === 'undefined'); const isBoolean = (value) => (typeof value === 'boolean'); const isDate = (value) => (value instanceof Date); const isRegExp = (value) => (value && typeof value === 'object' && value.constructor === RegExp); const isError = (value) => (value instanceof Error && typeof value.message !== 'undefined'); const isSymbol = (value) => (typeof value === 'symbol'); const isPlainSybol =(value) => value ? value.constructor === {}.constructor : false; </script>
正如评论中提到的,对象没有排序,所以你不能对对象进行排序。 您可以对数组进行排序。 如果你想对某些东西进行排序,你需要一个比较的值。 换句话说,您需要确定一个值是大于、小于还是等于另一个值。 最简单的方法是为每种类型分配一个数字,表示排序顺序和基于该排序的排序。 例如,这是一个将类型映射到数字的简单(且不完整)函数:
function sortValue(item) {
let order = {
'number': 3,
'string': 2,
'object': 0
}
if (Array.isArray(item)) return 1
else if (typeof item in order) return order[typeof item]
else return -1
}
有了它,您可以对您的值(或基于值的键等)进行排序。 当然,这将返回一个数组,因为与对象不同的数组是有序的:
const myObject = { "id": 3, "name": "my object", "content": ["a", "b", "c"], "description": "my description", "anotherArray": [11, 22, 33], "contentObject": { "id": 56, "name": "content object" } } function sortValue(item) { let order = { 'number': 3, 'string': 2, 'object': 0 } if (Array.isArray(item)) return 1 else if (typeof item in order) return order[typeof item] else return -1 } let sortedValues = Object.values(myObject).sort((a, b) => sortValue(b) - sortValue(a)) console.log(sortedValues)
Ypou8 可以通过检查类型并使用数值(索引)进行排序来重建对象。 未知类型被排序到数组的末尾以重建对象。
function rebuildObject(object) { const isNumber = v => typeof v === 'number' && isFinite(v), isString = v => typeof v === 'string' || v instanceof String, isArray = v => Array.isArray(v), isObject = v => v && typeof v === 'object', getOrder = v => [isNumber, isString, isArray, isObject].findIndex(fn => fn(v)) + 1 || Infinity; return Object.assign( ...Object .entries(object) .sort(({ 1: a }, { 1: b }) => getOrder(a) - getOrder(b)) .map(([k, v]) => ({ [k]: v })) ); } console.log(rebuildObject({ id: 3, name: "my object", content: ["a", "b", "c"], description: "my description", anotherArray: [11, 22, 33], contentObject: { id: 56, name: "content object" } }));
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.