javascript - 如何让 Lodash orderBy 只对有数据的记录进行排序?
问题描述
我有一个包含以下元素的数组,
let stats = [
{ 'keyword': 'testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing software', 'searches': 235, 'isAnalysed': true },
{ 'keyword': 'the testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing the boundries', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing business ideas', 'searches': 155, 'isAnalysed': true },
{ 'keyword': 'testing electronics', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing and assessment', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing microservices', 'searches': 65, 'isAnalysed': true },
];
我想对数组进行排序,使其看起来像这样,
升序;
let stats = [
{ 'keyword': 'testing microservices', 'searches': 65, 'isAnalysed': true },
{ 'keyword': 'testing business ideas', 'searches': 155, 'isAnalysed': true },
{ 'keyword': 'testing software', 'searches': 235, 'isAnalysed': true },
{ 'keyword': 'testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'the testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing the boundries', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing electronics', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing and assessment', 'searches': 0, 'isAnalysed': false },
];
降序;
let stats = [
{ 'keyword': 'testing software', 'searches': 235, 'isAnalysed': true },
{ 'keyword': 'testing business ideas', 'searches': 155, 'isAnalysed': true },
{ 'keyword': 'testing microservices', 'searches': 65, 'isAnalysed': true },
{ 'keyword': 'testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'the testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing the boundries', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing electronics', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing and assessment', 'searches': 0, 'isAnalysed': false },
];
是的,我想按searches
if that object排序isAnalysed = true
。我正在使用Lodash orderBy()
。这是我为此编写的排序代码段。
// direction - asc/desc
const sortedData = _.orderBy(stats, [function (object) {
if (object.isAnalysed === true) return object.searches;
}], [direction]);
但这会对整个数组进行排序,就像由searches
. 我在这里想念什么?
解决方案
如果您不必使用 lodash 类似这样的东西应该可以解决它:
const stats = [
{ 'keyword': 'testing software', 'searches': 235, 'isAnalysed': true },
{ 'keyword': 'testing business ideas', 'searches': 155, 'isAnalysed': true },
{ 'keyword': 'testing microservices', 'searches': 65, 'isAnalysed': true },
{ 'keyword': 'testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'the testing', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing the boundries', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing electronics', 'searches': 0, 'isAnalysed': false },
{ 'keyword': 'testing and assessment', 'searches': 0, 'isAnalysed': false },
];
const statsAnalysed = stats.filter(stat => stat.isAnalysed);
const statsNotAnalysed = stats.filter(stat => !stat.isAnalysed);
const statsAnalysedSorted = (order) => statsAnalysed.sort((s1, s2) => order === 'asc' ? s1.searches - s2.searches : s2.searches - s1.searches);
console.log(statsAnalysedSorted('asc').concat(statsNotAnalysed));
推荐阅读
- r - 通过过滤和子集单独的数据框创建数据框?
- javascript - 如何不将横幅与 HTML/CSS 中的文本和图像重叠?
- git - 如何恢复存储库以提交
- java - 如何在所有数字和总和的java eclipse平方中解决这个问题
- c++ - 是否可以将 wchar_t* 字符串作为新的字符串复制到向量中?
- sql-server - 如何在集群模式下使用 keytab 使 Spark jdbc 连接到 Sqlserver
- python - LSTM 中的交叉验证 - ValueError:层序号_3 的输入 0 与层不兼容
- java - 永远不会调用paintComponent()
- java - Netbeans 没有安装在我的 Mac 中,
- python - 如何重新启动 Kivy 动画