![](/img/trans.png)
[英]Use lodash _.sortby to sort an array of contacts with details and send null values last?
[英]Use _sortBy of lodash to sort an array by a different field
具有這樣的對象數組:
myArray = [
{AType: "aaa", Description: "De", …},
{AType: "bbb", Description: "Hi", …},
{AType: "ccc", Description: "Un", …},
{AType: "ddd", Description: "Hw", …},
];
它按AType
排序,但我想按Description
排序。
import _sortBy from 'lodash/sortBy';
mySortedArray = _sortBy(myArray, s => s.Description);
它沒有達到我的預期,結果看起來像: [Array(4), Array(3), {…}, {…}]
有什么想法如何通過該字段對其進行排序,但又不修改數組中的其他內容嗎?
不知道問題出在哪里。 同樣對於這種簡單的排序,您不需要箭頭功能,只需屬性名稱。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.