gpt4 book ai didi

javascript - 显示一个对象的多个部分?

转载 作者:行者123 更新时间:2023-11-29 19:11:46 25 4
gpt4 key购买 nike

这个对象给出了分数和工作名称:

   var job0= {
score: 100,
name: "Project Manager"
}


$('#scoreTest').text(job0.score +' ' + job0.name)

我正在尝试让职位 96-100 也成为具有上述职位名称和分数的对象。问题是我无法通过数组传递作业名称以使用上述对象策略找到 3 个最高分数。我只能通过分数#。我将如何通过作业名称传递#highest1、#Highest2、#Highest3 也会显示作业名称?

var job95 = 100;
var job96 = 100;
var job97 = 100;
var job98 = 100;
var job99 = 100;
var job100 = 100;


var numArray = [job95,job96, job97, job98, job99,job100]
$('#highest').html(Math.max.apply(Math,numArray));

numArray.sort(function(a, b) {
return a - b;
});
//Get the highest 3
var top3Arr = numArray.slice(-3);

var firsthighest = top3Arr[2] ;
var secondHighest = top3Arr[1];
var thirdHighest = top3Arr[0];

$('#highest1').html(firsthighest +' '+ job0.name)
$('#highest2').html(secondHighest)
$('#highest3').html(thirdHighest)
});

最佳答案

您可以直接为对象使用数组。

var jobs = [{
score: 10,
name: "Project Manager"
}, {
score: 20,
name: "Team Leader"
}, {
score: 42,
name: "Worker"
}, {
score: 99,
name: "Worker"
}, {
score: 100,
name: "Worker"
}],
top3;

jobs.sort(function (a, b) { return b.score - a.score; });
top3 = jobs.slice(0, 3);

console.log(top3);

关于javascript - 显示一个对象的多个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38334812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com