You can do it by sorting the array desc
order then grab the first index value.
var assignments = [
{
moduleCode: "346",
moduleName: "Computer Science",
quota: 100,
dueDate: "2019-12-12"
},
{
moduleCode: "360",
moduleName: "Maths",
quota: 200,
dueDate: "2020-05-01"
}
];
assignments.sort((a,b) => b.quota - a.quota);
console.log(assignments[0]);
You are just assigning the object property value to highestQuota
with highestQuota = assignments[i].quota;
. If you want highestQuota
to be assigned an object, it needs to be initialized as an object first, and then you need to add the entire assignments object, and not just it’s value.
let highestQuota = {}
highestQuota = assignments[i]
I’m writing a program that schedules assignments with a number of hours (quota) to meet by the deadline (dueDate). It is based on an array of assignment objects.
var assignments = [m1 = {
moduleCode: "346",
moduleName: "Computer Science",
quota: 100,
dueDate: "2019-12-12"
}, m2 = {
moduleCode: "360",
moduleName: "Maths",
quota: 200,
dueDate: "2020-05-01"
}];
I’m writing a functions to retrieve an object based on certain parameters, e.g. the one with the highest number of hours, but I want to return the object as a whole, instead of just the property.
function getHighestWorkload(assignments) {
let highestQuota = 0;
for(var i=0; i<assignments.length; i++) {
if(assignments[i].quota > highestQuota) {
highestQuota = assignments[i].quota;
}
}
// Get the entire assignment object based on its quota
}
Is there a way to return the entire object based on its parameter?
Thank you.