带有空值和实值的lodash orderby排序不正确 [英] lodash orderby with null and real values not ordering correctly

查看:347
本文介绍了带有空值和实值的lodash orderby排序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular 2打字稿应用程序,它使用lodash处理各种事情.

I have an Angular 2 typescript application that is using lodash for various things.

我有一个对象数组,正在使用对象中的属性进行排序...

I have an array of objects that I am ordering using a property in the object...

_.orderBy(this.myArray, ['propertyName'], ['desc']);

这很好用,但是我的问题是有时'propertyName'可以有一个空值. 这些是作为降序列表中的第一项排序的,然后是最高的实际值.

This works well however my problem is that sometimes 'propertyName' can have a null value. These are ordered as the first item in a descending list, the highest real values then follow.

我想使这些空值以降序显示在最后.

I want to make these null values appear last in the descending ordering.

我理解为什么空值首先出现.

I understand why the nulls come first.

有人知道如何解决这个问题吗?

Does anyone know how to approach this?

推荐答案

我需要的代码如下:

_.orderBy(this.myArray, [( o ) => { return o.myProperty || ''}], ['desc']); 

这篇关于带有空值和实值的lodash orderby排序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆