Question

I am trying to set up an associative array for the following data:

name       date        alpha    beta
Andrew    12/08/07      2.3     1.4
            5/12/07         
            26/03/08    
____________________________________
Fred    3/09/07         2.1 1.1
        23/01/08
____________________________________

Basically, each patient would have a name and alpha , beta value but multiple dates on which they visited doctor. I was thinking of something like following where name is the primary key and dates are stored in an array and alpha, beta is a float value associated with the key.

var info = [{ name: [{ dates: [ ], alpha: float, beta: float  }] }];

and then this info array would be populated on reading the csv file. What would be the right format for initialising such an associative array? or what other data structure would be a good approach for representing such a data?

Thanks in advance!

Was it helpful?

Solution

Edit: Since each patient has a unique name, instead of using an array, you should consider using a single object where each patient is an object identified by the object key, for example:

var patientList = {
  andy: {},
  bob: {}
};

To get your data from your CSV file into this structure you might consider something like this:

var csv = 'Andrew\t12/08/07\t1.2\t3.4\nAndrew\t15/09/08\t1.2\t3.4\nAndrew\t14/08/07\t1.2\t3.4\t\nBob\t18/09/08\t1.2\t3.4\nAndrew\t21/08/07\t1.2\t3.4\nDavid\t31/09/08\t1.2\t3.4\nAndrew\t22/08/07\t1.2\t3.4\t\nSam\t26/09/08\t1.2\t3.4';

// Split the CSV file at the carriage return.
var data = csv.split('\n');

// Recursive over the data with `map`, splitting each line up
// on the tabs and returning a patient object for each.
data = data.map(function (el) {
  var patient = el.split('\t');
  return {
    name: patient[0],
    date: patient[1],
    alpha: patient[2],
    beta: patient[3]
  }
});


function getListOfPatientNames(arr) {
  var newarr = [];

  // For each patient object return the patient name only
  newarr = arr.map(function (patient) {
    return patient.name;
  });

  // A quick way of filtering out duplicates
  return newarr.filter(function(elem, pos) {
    return newarr.indexOf(elem) == pos;
  });
}

// Return a unique list of names, and sort them.
var names = getListOfPatientNames(data).sort();

var patientList = {};

for (var i = 0, l = data.length; i < l; i++) {
  var name = data[i].name;

  // If the patient name doesn't exist in patientList yet
  if (!patientList[name]) {

    // Add a new patient object using the name as the key
    var newPatient = {
      dates: [data[i].date],
      alpha: data[i].alpha,
      beta: data[i].beta
    };
    patientList[name] = newPatient;
  } else {

    // If the patient already exists push the date to the dates array
    patientList[name].dates.push(data[i].date);
  }
}

Demo

OTHER TIPS

The term "associative array" is almost never used wrt JavaScript; you use objects (sometimes called "maps" or "dictionaries") for name/value information, or arrays for ordered data.

It looks like you want an array of patient objects, like this:

var patients = [
    {
        name: "Andrew",
        dates: [/*...the dates...*/],
        alpha: 2.3,
        beta: 1.4
    },
    {
        name: "Fred",
        dates: [/*...the dates...*/],
        alpha: 2.1,
        beta: 1.1
    }
];

You might or might not want to use a constructor function to create those objects, depending on your needs, but with the simple data you've given there's probably no need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top