我想获得scores
对象的最高价值。
我有一个全局对象“ implements”:
[
{
"id": 5,
"project": 'name project',
"scores": [
{
"id": 7,
"user_id": 30,
"implementation_id": 5,
"score": "12.00",
"comment": "C'est de la merde, ça vient d’Anthony."
},
{
"id": 10,
"user_id": 31,
"implementation_id": 5,
"score": "15.00",
"comment": "Super"
}
]
},
{
"id": 6,
"project": 'name project',
"scores": [
{
"id": 8,
"user_id": 30,
"implementation_id": 6,
"score": "20.00",
"comment": "Super!"
},
{
"id": 11,
"user_id": 31,
"implementation_id": 6,
"score": "12.00",
"comment": "Super"
},
{
"id": 15,
"user_id": 36,
"implementation_id": 6,
"score": "10.00",
"comment": "Super"
}
]
}
]
例如,在上述对象中,对象1有2分,而对象2有3分。
我想要两者的最大价值。如果我们添加第三个对象,并且得到5分,我想得到5分。
如何使用javascript函数做到这一点?
谢谢
答案 0 :(得分:1)
最简单的方法是创建一个虚拟值并计算计数。然后对其进行排序。
const scoresList = [
{
"id": 5,
"project": 'name project',
"scores": [
{
"id": 7,
"user_id": 30,
"implementation_id": 5,
"score": "12.00",
"comment": "C'est de la merde, ça vient d’Anthony."
},
{
"id": 10,
"user_id": 31,
"implementation_id": 5,
"score": "15.00",
"comment": "Super"
}
]
},
{
"id": 6,
"project": 'name project',
"scores": [
{
"id": 8,
"user_id": 30,
"implementation_id": 6,
"score": "20.00",
"comment": "Super!"
},
{
"id": 11,
"user_id": 31,
"implementation_id": 6,
"score": "12.00",
"comment": "Super"
},
{
"id": 15,
"user_id": 36,
"implementation_id": 6,
"score": "10.00",
"comment": "Super"
}
]
}
];
for(var i = 0; i < scoresList.length; i++){
//create a dummy variable scoreCount
scoresList[i].scoreCount = 0;
if(scoresList[i].scores){
scoresList[i].scoreCount = scoresList[i].scores.length;
}
}
scoresList.sort(function(a, b){
return b.scoreCount - a.scoreCount
});
console.log(scoresList[0]); //This will give you the score with highest count
答案 1 :(得分:0)
我个人认为,这些任务可以由纯JS
处理假设scores
保存JSON数据:
const groups = scores.map(item => item.scores)
const lengths = groups.map(set => set.length)
const max = Math.max(...lengths)
相当优雅(至少在我看来)
旁注:
使用以下方法检索包含结果的“平面”对象:
const flatResults = Array.prototype.concat.apply([],scores.map(item => item.scores)))
答案 2 :(得分:0)
您可以将分数map设置为数字数组:data.map(item=>item.scores.map(x=>x.score).map(Number))
然后再次使用reduce映射以获取每个嵌套数组中的最高值:.map(scores=>scores.reduce((result,score)=>Math.max(result,score),0))
。每个数据项得分最高。
如果您希望得分最高,可以flat并减少:.flat().reduce((result,score)=>Math.max(result,score),0)
const data = [{"id":5,"project":"name project","scores":[{"id":7,"user_id":30,"implementation_id":5,"score":"12.00","comment":"C'est de la merde, ça vient d’Anthony."},{"id":10,"user_id":31,"implementation_id":5,"score":"15.00","comment":"Super"}]},{"id":6,"project":"name project","scores":[{"id":8,"user_id":30,"implementation_id":6,"score":"20.00","comment":"Super!"},{"id":11,"user_id":31,"implementation_id":6,"score":"12.00","comment":"Super"},{"id":15,"user_id":36,"implementation_id":6,"score":"10.00","comment":"Super"}]}];
console.log(
'highest per data item:',
data.map(item=>item.scores.map(x=>x.score).map(Number))
.map(scores=>scores.reduce((result,score)=>Math.max(result,score),0))
);
console.log(
'highest over all:',
data.map(item=>item.scores.map(x=>x.score).map(Number))
.flat().reduce((result,score)=>Math.max(result,score),0)
);