最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lodash: order array of objects by boolean? - Stack Overflow

programmeradmin3浏览0评论

I have an array of objects I'd like to order (essentially it's a table):

myArr = [{
    name: 'John',
    email: '[email protected]',
    accepted: true
}, {
    name: 'Alfred',
    email: '[email protected]',
    accepted: false
}]

I'm using orderBy from lodash like so:

//get columnName to sort by from another function
const newArr = _.orderBy(myArr, [columnName], ['asc'])

Ordering by name and email works fine, for accepted it doesn't do anything though. I understand I can store accepted as 0 and 1, but is there another way? Is lodash sufficient for that, or should I create a separate function for this?

I have an array of objects I'd like to order (essentially it's a table):

myArr = [{
    name: 'John',
    email: '[email protected]',
    accepted: true
}, {
    name: 'Alfred',
    email: '[email protected]',
    accepted: false
}]

I'm using orderBy from lodash like so:

//get columnName to sort by from another function
const newArr = _.orderBy(myArr, [columnName], ['asc'])

Ordering by name and email works fine, for accepted it doesn't do anything though. I understand I can store accepted as 0 and 1, but is there another way? Is lodash sufficient for that, or should I create a separate function for this?

Share Improve this question edited Oct 22, 2017 at 12:18 ibrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges asked Oct 22, 2017 at 12:15 RunnickRunnick 7154 gold badges16 silver badges36 bronze badges 5
  • 3 It works fine - fiddle – Ori Drori Commented Oct 22, 2017 at 12:21
  • Stupid me! There is something with my view layer then... – Runnick Commented Oct 22, 2017 at 12:25
  • 1 Enjoy the view :) – Ori Drori Commented Oct 22, 2017 at 12:26
  • 8 For anyone stumbling upon this question: be aware that because you're sorting a boolean, you're actually sorting 0 or 1. If you'd like the objects with 'true' to be on top, you should specify that it should sort desc (0/false is going to be on top otherwise). – Erwin Lengkeek Commented Feb 27, 2019 at 7:00
  • @Erwin Lengkeek - i tried with 3 boolean fields - doesnt work - i think lodash cannot handle this situtaion :( – moshiah_now Commented Nov 12, 2020 at 13:02
Add a ment  | 

3 Answers 3

Reset to default 6

You can try this in ES6 Notation

const newArr = [
    ...myArr.filter(c => c.accepted === false),
    ...myArr.filter(c => c.accepted === true)
];

You can also use Array.sort Prototype

myArr.sort((a, b) => (a.accepted - b.accepted));

@Erwin Lengkeek that worked for me, by sorting my booleanField descending, the Trues were on top.

this.sortableList = orderBy(this.sortableList, ['booleanField', 'stringField'], ['desc', 'asc']);

I've tried @Murtaza Mehmudji solution & it works fine!

const newArr = [
    ...myArr.filter(c => c.accepted === false),
    ...myArr.filter(c => c.accepted === true)
];
发布评论

评论列表(0)

  1. 暂无评论