拥有这样的对象数组:
myArray = [
{AType: "aaa", Description: "De", …},
{AType: "bbb", Description: "Hi", …},
{AType: "ccc", Description: "Un", …},
{AType: "ddd", Description: "Hw", …},
];
按AType
排序,但我想按Description
排序。
我尝试使用sortBy from lodash:
import _sortBy from 'lodash/sortBy';
mySortedArray = _sortBy(myArray, s => s.Description);
它不符合我的预期,结果如下:[Array(4), Array(3), {…}, {…}]
任何想法如何按该字段对其进行排序,但也不修改数组中的任何其他内容?
答案 0 :(得分:1)
不确定问题是什么。另外,对于这样的简单排序,您不需要箭头功能,只需要属性名称。
myArray = [
{AType: "aaa", Description: "De"},
{AType: "bbb", Description: "Hi"},
{AType: "ccc", Description: "Un"},
{AType: "ddd", Description: "Hw"}
];
mySortedArray = _.sortBy(myArray, 'Description');
console.log(mySortedArray);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
&#13;