3

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:"cornflakes123@gmail.com"
   employee_name:"Mark"
   merits:10
},
{
   id:"12"
   email:"fourleaves@gmail.com"
   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.

Macy Creed
  • 57
  • 5

2 Answers2

1

UPDATE 2

Getting the median with the following function (thanks goes to https://stackoverflow.com/a/53660837/8820118):

let data = [
      {
        id: "15",
        email: "cornflakes123@gmail.com",
        employee_name: "Mark",
        merits: 1,
      },
      {
        id: "12",
        email: "fourleaves@gmail.com",
        employee_name: "Grey",
        merits: 5,
      },
            {
        id: "12",
        email: "fourleaves@gmail.com",
        employee_name: "Grey",
        merits: 100,
      },
            {
        id: "12",
        email: "fourleaves@gmail.com",
        employee_name: "Grey",
        merits: 100,
      },
                  {
        id: "12",
        email: "fourleaves@gmail.com",
        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: "cornflakes123@gmail.com",
        employee_name: "Mark",
        merits: 10,
      },
      {
        id: "12",
        email: "fourleaves@gmail.com",
        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.

nosTa
  • 607
  • 6
  • 16
  • Actually I have put this to a javascript transformer so it kindof looks like this 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; } it somehow only throws the very first row i have selected and not adding up the other three (example) that i have additionally selected. can you please let me know what's to add – Macy Creed Sep 21 '22 at 09:41
  • what are you using? angular? And please edit your first post and provide all the code you are using so we can see more informations :) – nosTa Sep 21 '22 at 09:42
  • check my update – nosTa Sep 21 '22 at 10:20
  • hi nosTa! Thank you for this! if i may be looking for the median for the selected rows, for example given are 5, 11, and 12 and wishing to get the middle part which is 11, is it still the same as your provided? how so – Macy Creed Sep 21 '22 at 10:34
  • so if you have 5 numbers, 1, 5, 10, 100, 500 your desired output is 10? – nosTa Sep 21 '22 at 10:39
  • Yes, even if the given row is on random like 500, 100, 1, 5, 10 and still get 10 – Macy Creed Sep 21 '22 at 10:42
  • check my second update. – nosTa Sep 21 '22 at 10:45
  • 1
    thank you so much! @nosTa! worked out well. i'll carry on the rest – Macy Creed Sep 21 '22 at 10:57
0
employeesTable.selectedRow.data.forEach((ele)=>{
  console.log(ele.id)
})
HamzaElkotb
  • 138
  • 1
  • 7