I have a code for fetching multiple selected rows. I can get all the rows of data but I am planning to get specific data that is merited. I'm stuck on getting the specific value of merits of each selected row and wanting to do code for their median. Please advise.
const median = {{ employeesTable.selectedRow.data }}
return median
Here is a sample data and what I'd like to get is only the merits which is 10 and 14 for example and get their median.
[{
id:"15"
email:"[email protected]"
employee_name:"Mark"
merits:10
},
{
id:"12"
email:"[email protected]"
employee_name:"Grey"
merits:14
}]
My desired output to be showing is the median of the merit/s being selected. For example if three rows are selected, their merits are gathered and then given a median.
Example, if the three rows are having the merits of 5, 11 and 12 then the my desired output is to show the median 11 on ''return'' value.
CodePudding user response:
UPDATE 2
Getting the median with the following function (thanks goes to https://stackoverflow.com/a/53660837/8820118):
let data = [
{
id: "15",
email: "[email protected]",
employee_name: "Mark",
merits: 1,
},
{
id: "12",
email: "[email protected]",
employee_name: "Grey",
merits: 5,
},
{
id: "12",
email: "[email protected]",
employee_name: "Grey",
merits: 100,
},
{
id: "12",
email: "[email protected]",
employee_name: "Grey",
merits: 100,
},
{
id: "12",
email: "[email protected]",
employee_name: "Grey",
merits: 500,
},
]
function median(myData) {
const sorted = Array.from(myData.map((item) => item.merits)).sort((a, b) => a - b);
const middle = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
return (sorted[middle - 1] sorted[middle]) / 2;
}
return sorted[middle];
}
console.log(median(data));
UPDATE
Based on your comment you want to return the lowest merit value from your array.
const data = {{ employeesTable.selectedRow.data }}
//now that we have the data we can get min and max value
let min = Math.min(...data.map(item => item.merits));
let max = Math.max(...data.map(item => item.merits));
return min; //or return max
OLD POST
Just select the key from your object. Here is a little example based on your provided code:
const employeesTable = {
selectedRow: {
data: [
{
id: "15",
email: "[email protected]",
employee_name: "Mark",
merits: 10,
},
{
id: "12",
email: "[email protected]",
employee_name: "Grey",
merits: 14,
},
],
},
};
//iterate over the array
for (let i = 0; i < employeesTable.selectedRow.data.length; i ) {
console.log(getMerits(i)); //return merit for that entry
}
function getMerits(index) {
return employeesTable.selectedRow.data[index].merits;
}
Please give more details as your question can mean something else, for example only retrieving those with value 10 and 14. In that case provide more informations what you are trying to achieve.
CodePudding user response:
employeesTable.selectedRow.data.forEach((ele)=>{
console.log(ele.id)
})