如何以lodash顺序忽略换行

时间:2018-09-19 14:27:05

标签: javascript node.js lodash

我正在使用lodash / orderBy订购一个数组。我无法控制阵列中返回的内容。当我的数组项中包含换行时,则它们不会按我期望的顺序排序。有什么办法可以忽略新行吗?还是通过其他方式正确订购商品?

const shouldBeFirst = 'My message\r\n\r\nshould consist of A A A A some 
text';
const shouldBeSecond= 'My message\r\n\r\nshould consist of \r\n\r\n some 
text';

const array = [
 { text: 'xxx' },
 { text: shouldBeFirst },
 { text: 'yyy' },
 { text: shouldBeSecond},
 { text: 'zzz' }];

const ordered = orderBy(array, ['day'], ['asc']);

我希望得到这些物品的顺序是

 { text: shouldBeFirst },
 { text: shouldBeSecond},
 { text: 'xxx' },
 { text: 'yyy' },
 { text: 'zzz' }];

但是我得到它们的顺序是:

 { text: shouldBeSecond },
 { text: shouldBeFirst },,
 { text: 'xxx' },
 { text: 'yyy' },
 { text: 'zzz' }];

[edit:实际上,我需要按更多字段进行排序,因此实际排序看起来更像下面的代码]

 const array = [
 { text: 'xxx', day: 'monday', hour: '12' },
 { text: shouldBeFirst, day: 'tuesday', hour: '12' },
 { text: 'yyy', day: 'wednesday', hour: '12' },
 { text: shouldBeSecond, day: 'thursday', hour: '12'},
 { text: 'zzz', day: 'friday', hour: '12' }];

const ordered = orderBy(array, ['day', 'hour', 'text'], ['asc', 'asc', 'asc']);

2 个答案:

答案 0 :(得分:1)

编辑:修订了我的答案-确实可以使用orderBy

是的,您可以使用orderBy来提供比较功能。

const shouldBeFirst = 'My message\r\n\r\nshould consist of A A A A some text';
const shouldBeSecond= 'My message\r\n\r\nshould consist of \r\n\r\n some text';

const array = [
 { text: 'xxx' },
 { text: shouldBeFirst },
 { text: 'yyy' },
 { text: shouldBeSecond},
 { text: 'zzz' }];

const ordered = _.orderBy(array, item => item.text.replace(/\s+/g, " "));

console.log(ordered)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

如果需要,可以调整替换项-这只是为了说明如何定义替换项。仍然可以将多个条件作为数组传递

const shouldBeFirst = 'My message\r\n\r\nshould consist of A A A A some text';
const shouldBeSecond= 'My message\r\n\r\nshould consist of \r\n\r\n some text';

const array = [
 { text: 'xxx', value: 2 },
 { text: 'xxx', value: 1 },
 { text: shouldBeFirst },
 { text: 'yyy', value: 42 },
 { text: 'yyy', value: 12 },
 { text: shouldBeSecond},
 { text: 'zzz', value: 7 }];

//separating criteria for better readability
const criteria1 = item => item.text.replace(/\s+/g, " ");
const criteria2 = 'value'; //you can still pick by property name

//order by sanitized values of the text property first, followed by the value property
const ordered = _.orderBy(array, [criteria1, criteria2]);

console.log(ordered);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

答案 1 :(得分:1)

改为使用_.sortBy。您可以在订购前映射值:

const ordered = _.sortBy(array, arrayItem => arrayItem.text.replace(/\W+/g, " "));

/\W+/g是一个正则表达式,用于在比较之前从数组项中删除所有非字母数字字符。

而且,如果您想按多个值排序:

const ordered = _(array).chain()
                        .sortBy(arrayItem => arrayItem.day)
                        .sortBy(arrayItem => arrayItem.hour)
                        .sortBy(arrayItem => arrayItem.text.replace(/\W+/g, " "))
                        .value();

但是,这将按字母顺序对工作日进行排序,而不是按一周中的顺序进行排序-您始终可以使用moment库获取工作日的索引并将其返回。