I should have mentioned using Underscore's
groupBy() method which makes this sooooooooo easy to do. Once you read your
CSV data from the selected file and convert it to an array of arrays you have something like this:
var csv = [
["ID101","Jane","Hydrocodone","30","1/1/2019","lorem ipsum"],
["ID101","Jane","Levothyroxine","10","1/1/2019","dolor sit"],
["ID101","Jane","Prednisone","20","1/1/2019","amet consectetur"],
["ID201","John","Amoxicillin","60","1/2/2019","adipiscing elit"],
["ID201","John","Gabapentin","60","1/3/2019","Aliquam gravida"],
["ID201","John","Lisinopril","10","1/4/2019","sollic tincidunt"],
["ID301","Mary","Atorvastatin","20","1/6/2019","Nam enim"],
["ID301","Mary","Metformin","40","1/7/2019","mi mattis"],
["ID301","Mary","Ondansetron","10","1/7/2019","vel molestie"],
["ID301","Mary","Ibuprofen","10","1/9/2019","id porta"]
];
With this one statement you can group by the
[PatientID] in the 0'th column:
var groupedData = _.groupBy(csv, 0);
console.dir(JSON.stringify(groupedData, null, " "));
Now you have identified the three groups ("ID101", "ID201", ID301") by [PatientID] and can
easily pluck out the information that should go into the
Patients table and the information that should go into the related
Prescriptions table.
You might be able to visualize this better if you copy and paste the following
JSON into a
JSON visualization tool such as
http://jsonviewer.stack.hu/{
"ID101": [
[
"ID101",
"Jane",
"Hydrocodone",
"30",
"1/1/2019",
"lorem ipsum"
],
[
"ID101",
"Jane",
"Levothyroxine",
"10",
"1/1/2019",
"dolor sit"
],
[
"ID101",
"Jane",
"Prednisone",
"20",
"1/1/2019",
"amet consectetur"
]
],
"ID201": [
[
"ID201",
"John",
"Amoxicillin",
"60",
"1/2/2019",
"adipiscing elit"
],
[
"ID201",
"John",
"Gabapentin",
"60",
"1/3/2019",
"Aliquam gravida"
],
[
"ID201",
"John",
"Lisinopril",
"10",
"1/4/2019",
"sollic tincidunt"
]
],
"ID301": [
[
"ID301",
"Mary",
"Atorvastatin",
"20",
"1/6/2019",
"Nam enim"
],
[
"ID301",
"Mary",
"Metformin",
"40",
"1/7/2019",
"mi mattis"
],
[
"ID301",
"Mary",
"Ondansetron",
"10",
"1/7/2019",
"vel molestie"
],
[
"ID301",
"Mary",
"Ibuprofen",
"10",
"1/9/2019",
"id porta"
]
]
}