如何从数组中删除此类条目
然后将输出排序并限制为仅有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"}
]
答案 0 :(得分:2)
你不需要使用下划线来执行此操作,你可以在普通的javascript中执行此操作,因为本机方法更快,所以这样做更有效。要做的就是过滤出假名值,然后按Costs
排序,最后切出前7个结果:
注意使用-
,check out here why
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()