0
在JavaScript的我有以下阵列对象:如何按属性值长度对数组进行排序?
var defaultSanitizer = [
{"word": "large", "replaceWith":"L"},
{"word": "os", "replaceWith":"One Size"},
{"word": "xlarge", "replaceWith":"XL"},
{"word": "o/s", "replaceWith":"One Size"},
{"word": "medium", "replaceWith":"M"}
...
];
(实际上这阵列大得多)
我想使一个函数,所以我可以订购通过属性的长度的阵列价值例如对象的属性“单词”。
事情是这样的:
function sortArrByPropLengthAscending(arr, property) {
var sortedArr = [];
//some code
return sortedArr;
}
如果我要运行函数sortArrByPropLengthAscending(defaultSanitizer, “字”),它应该返回我的有序排列,看起来像这样:
sortedArr = [
{"word": "os", "replaceWith":"One Size"},
{"word": "o/s", "replaceWith":"One Size"},
{"word": "large", "replaceWith":"L"},
{"word": "xlarge", "replaceWith":"XL"},
{"word": "medium", "replaceWith":"M"}
...
]
如何你会这样做吗?
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort –