简体   繁体   English

js:带有关联数组的数组

[英]js: array with in an associative array

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. 我想的是跟随名称是主键,日期存储在数组和alpha中,beta是与键相关联的浮点值。

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

and then this info array would be populated on reading the csv file. 然后在读取csv文件时填充此信息数组。 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! 提前致谢!

The term "associative array" is almost never used wrt JavaScript; 术语“关联数组”几乎从不用于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. 您可能会或可能不想使用构造函数来创建这些对象,具体取决于您的需要,但是您可能没有必要使用您提供的简单数据。

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: 要将CSV文件中的数据导入此结构,您可能会考虑以下内容:

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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM