lodash - 根据不同键的值排序?

时间:2017-06-09 18:10:43

标签: lodash

我有一个对象数组,其数量(gradle jar incrementalPublishToMavenRepository Configuration on demand is an incubating feature. :core:compileJava UP-TO-DATE :core:processResources NO-SOURCE :core:classes UP-TO-DATE :core:jar UP-TO-DATE :app:compileJava :app:processResources UP-TO-DATE :app:classes :app:jar :app:generatePomFileForMavenPublication :app:incrementalPublishToMavenRepository Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar.sha1 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar.md5 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom.sha1 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom.md5 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml.sha1 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml.md5 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml.sha1 Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml.md5 hello this should be executed ones :core:generatePomFileForMavenPublication :core:incrementalPublishToMavenRepository UP-TO-DATE / fruit_quantity):

我正在尝试按这些数量排序,因此它显示了lodash从最高到最低的顺序。

vegetable_quantity

我知道如果它们是同一个密钥,比如var items = [ {'type': 'fruit', 'name': 'apple', 'fruit_quantity': 20}, {'type': 'fruit', 'name': 'banana','fruit_quantity': 10}, {'type': 'vegetable', 'name': 'brocolli','vegetable_quantity': 15}, {'type': 'fruit', 'name': 'cantaloupe','fruit_quantity': 5} ]; var sortedItems = _.sortBy(items, ['fruit_quantity' || 'vegetable_quantity']).reverse(); ,那么就会这样做:

quantity

但遗憾的是,我必须使用不同的密钥,但仍按其各自的数量排序。

这是我尝试的小提琴: https://jsfiddle.net/zg6js8af/1/

如何按最高到最低数量对商品进行排序?

这是最终排序顺序的样子:

var sortedItems = _.sortBy(items, 'quantity').reverse();

1 个答案:

答案 0 :(得分:3)

您只需要检测存在哪个属性,然后将其返回。使用Array#sort的示例:



var items = [
    {'type': 'fruit', 'name': 'apple', 'fruit_quantity': 20},
    {'type': 'fruit', 'name': 'banana','fruit_quantity': 10},
    {'type': 'vegetable', 'name': 'brocolli','vegetable_quantity': 15},
    {'type': 'fruit', 'name': 'cantaloupe','fruit_quantity': 5}
];

function getQuantity(item) {
  return item.fruit_quantity || item.vegetable_quantity || 0;
}

items.sort(function(a, b) {
  return getQuantity(a) - getQuantity(b);
});

console.log(items);




您可以对_.sortBy()执行相同操作:



var items = [
    {'type': 'fruit', 'name': 'apple', 'fruit_quantity': 20},
    {'type': 'fruit', 'name': 'banana','fruit_quantity': 10},
    {'type': 'vegetable', 'name': 'brocolli','vegetable_quantity': 15},
    {'type': 'fruit', 'name': 'cantaloupe','fruit_quantity': 5}
];

function getQuantity(item) {
  return item.fruit_quantity || item.vegetable_quantity || 0;
}

var result = _.sortBy(items, getQuantity);

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;