首页 > 解决方案 > 如何让 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 },
];

是的,我想按searchesif that object排序isAnalysed = true。我正在使用Lodash orderBy()。这是我为此编写的排序代码段。

  // direction - asc/desc
  const sortedData = _.orderBy(stats, [function (object) {
    if (object.isAnalysed === true) return object.searches;
  }], [direction]);

但这会对整个数组进行排序,就像由searches. 我在这里想念什么?

标签: javascriptsortinglodash

解决方案


如果您不必使用 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));


推荐阅读