0

I have a database with Canadian province and cumulative data entry for a disease notification. I would like to use my highest disease reporting for each province. Province Number of cases Cumulative number Quebec 1 1 Ontario 2 2 Quebec 1 2 Quebec 1 3 BC 4 4 BC 1 5 etc

This is the data I would like to get Quebec 3 Ontario 2 BC 5

Can anyone tell me how to code this in javascript. My data base is in Json. Please note that I have minimum coding experience THank you

Not sure if

  1. first only add all data from the first column and use result
  2. select highest value for each province from cumulative data
  3. select last data entry for each province
  • This sounds more like a database/querying question and you'll need to show what you have so far. I think you can likely write the query in JS, but you'll need to actually run it on your DB. – mykaf Nov 22 '22 at 19:16
  • If you please share a minimum set of entries from your JSON data it could be easier to show you how to process that data with javascript and return the expected result – Diego D Nov 22 '22 at 19:18
  • Because otherwise it would just be a description in steps.. while visiting the whole set, group the number of diseases by province and return such list. It’s not clear what’s cumulative number but probably you meant every notification to have a number and not to count as one. Anyway as you can see it’s hard to understand your words. You should go in bigger details to get a better help – Diego D Nov 22 '22 at 19:24
  • The word "database" can refer to a specific software tool that manages data, or a conceptual instance or collection of related data in one of those tools. When you say your "database is in JSON" do you mean all your data is in a JavaScript Object in the memory of your application? Or are you using some kind of database technology that stores objects in a JSON representation? – StriplingWarrior Nov 22 '22 at 19:51
  • Please format the question a little better. For example: Province, Number of cases, Cumulative number Quebec, 1, 1 Ontario, 2, 2 Quebec, 1, 2 Maybe also show us a sample of the json. – nettie Nov 22 '22 at 22:07

1 Answers1

0

Assuming that data are arrays of arrays as no keys were mentioned:

const data = [
  ["Quebec", 5],
  ["Quebec", 1],
  ["Quebec", 5],
  ["Ontario", 1],
  ["Ontario", 2],
  ["Ontario", 1]
];

function compare(a, b) {
  if (a[1] > b[1]) {
    return -1;
  }
  if (a[1] < b[1]) {
    return 1;
  }
  return 0;
}

const firstHighest = [];
// This variable is here both because I was lazy and it will run faster
const inserted = [] 

data.sort(compare).forEach((item) => {
  if(!inserted.includes(item[0])){
    firstHighest.push(item)
    inserted.push(item[0])
  }
})

console.log(firstHighest)

Unless you give exact specifications this is probably the most I can do. Glad to help with your homework.

MalwareMoon
  • 788
  • 1
  • 2
  • 13