从数组中删除无效键,并使用最高值进行排序和限制

时间:2016-03-09 19:40:47

标签: javascript underscore.js

如何从数组中删除此类条目

  • _id = 0
  • _id =""
  • _id = undefined
  • _id = null

然后将输出排序并限制为仅有7个成本最高的项目?

  

MYARRAY

[
{_id : "" , M : "4", S : "2", Costs: "6"},
{_id : "0", M : "1", S : "0", Costs: "1"},
{_id : "1", M : "1", S : "0", Costs: "1"},
{_id : "2", M : "1", S : "0", Costs: "1"},
{_id : "3", M : "0", S : "3", Costs: "3"},
{_id : "4",  M : "1", S : "0", Costs: "1"},
{_id : "5",  M : "1", S : "0", Costs: "5"},
{_id : "6",  M : "1", S : "0", Costs: "2"},
{_id : "7",  M : "1", S : "0", Costs: "5"},
{_id : "8",  M : "1", S : "0", Costs: "1"},
{_id : "9",  M : "1", S : "0", Costs: "10"}
]
  

期望的输出

[
{_id : "9",  M : "1", S : "0", Costs: "10"},
{_id : "7",  M : "1", S : "0", Costs: "5"},
{_id : "5",  M : "1", S : "0", Costs: "5"},
{_id : "3", M : "0", S : "3", Costs: "3"},
{_id : "6",  M : "1", S : "0", Costs: "2"},
{_id : "1", M : "1", S : "0", Costs: "1"},
{_id : "2", M : "1", S : "0", Costs: "1"}
]

3 个答案:

答案 0 :(得分:2)

你不需要使用下划线来执行此操作,你可以在普通的javascript中执行此操作,因为本机方法更快,所以这样做更有效。要做的就是过滤出假名值,然后按Costs排序,最后切出前7个结果:

注意使用-check out here why

的排序函数

使用普通JS

var top7 = myArray.filter(function(item) {
  return !!item; // filters out all falsey values
}).sort(function(a, b) {
  return parseInt(b.Costs) - parseInt(a.Costs); // sort descending by Costs
}).slice(0, 7); // extracts the top 7 results

运行示例:

var myArray = [
{_id : "" , M : "4", S : "2", Costs: "6"},
{_id : "0", M : "1", S : "0", Costs: "1"},
{_id : "1", M : "1", S : "0", Costs: "1"},
{_id : "2", M : "1", S : "0", Costs: "1"},
{_id : "3", M : "0", S : "3", Costs: "3"},
{_id : "4",  M : "1", S : "0", Costs: "1"},
{_id : "5",  M : "1", S : "0", Costs: "5"},
{_id : "6",  M : "1", S : "0", Costs: "2"},
{_id : "7",  M : "1", S : "0", Costs: "5"},
{_id : "8",  M : "1", S : "0", Costs: "1"},
{_id : "9",  M : "1", S : "0", Costs: "10"}
];

var top7 = myArray.filter(function(item) {
  return !!item; // filters out all falsey values
}).sort(function(a, b) {
  return parseInt(b.Costs) - parseInt(a.Costs); // sort descending by Costs
}).slice(0, 7);

console.log(top7);

使用下划线

注意:由于下划线只有sortBy方法总是按升序排序,我们必须按Costs的倒数排序才能达到降序:

// filter out all falsey values
var filtered = _.filter(myArray, function(item) {
  return !!item;
});

// sort descending by Costs
var sorted = _.sortBy(filtered, function(item) {
  return -parseInt(item.Costs); // Notice the '-' sign to achieve descending order
}); 

// slice the top 7 results 
var top7 = sorted.slice(0, 7);

运行示例:

var myArray = [
{_id : "" , M : "4", S : "2", Costs: "6"},
{_id : "0", M : "1", S : "0", Costs: "1"},
{_id : "1", M : "1", S : "0", Costs: "1"},
{_id : "2", M : "1", S : "0", Costs: "1"},
{_id : "3", M : "0", S : "3", Costs: "3"},
{_id : "4",  M : "1", S : "0", Costs: "1"},
{_id : "5",  M : "1", S : "0", Costs: "5"},
{_id : "6",  M : "1", S : "0", Costs: "2"},
{_id : "7",  M : "1", S : "0", Costs: "5"},
{_id : "8",  M : "1", S : "0", Costs: "1"},
{_id : "9",  M : "1", S : "0", Costs: "10"}
];

var filtered = _.filter(myArray, function(item) {
  return !!item; // filters out all falsey values
});

var sorted = _.sortBy(filtered, function(item) {
  return -parseInt(item.Costs); // sort descending by Costs
}); 

var top7 = sorted.slice(0, 7);

console.log(top7);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

答案 1 :(得分:1)

你可以filter然后sort然后slice将它们链接起来,如下所示。

var result = arr.filter(function(doc){
    return !!parseInt(doc._id);
}).sort(function(a,b){
    return (parseInt(b.Costs) - parseInt(a.Costs));
}).slice(0,7);

答案 2 :(得分:1)

使用下划线的另一个答案是使用reject删除ID无效的项目,sortBy对项目进行排序,使用first来限制结果集中的数字:

var invalidId = function(item){
    return item._id == "0" || item._id == "" || item._id == undefined || item._id == null;
}

var negateCosts = function(item){
    return -parseInt(item.Costs, 10)
}

var result = _.chain(data)
    .reject(invalidId)
    .sortBy(negateCosts)
    .first(7)
    .value()