alexc
alexc

Reputation: 1310

group multi-dimensional array based on one attribute javascript

I'm trying to loop through a multi-dimensional array to retrieve a set of values first based on the month, then after that creating another array inside arranged by question and the value associated with it.

I realise this doesn't make any sense, so here is a plnk;

http://plnkr.co/edit/fpc3fSvoVjhEtlWuuY6m?p=preview

If you check the console, the first console output from var data is the structure which I would like to achieve (with the parent array representing the date, and the 6 arrays within that representing the data).

The var newValue is currently being indexed, I would like to create an array with this output but for each of the dates within the data.

Here is the code;

d3.json("data.json", function(error, data) {

  var newValue = []

  if (error) {
    console.log(error);
  } else {
    data = data;
  }

  var dateS = "Jan_2016"
  var countryS = "Netherlands"

        for (var question = 0; question < data.questions.length; question++) {
          var country = data.countries.indexOf(countryS);
          var date = data.dates.indexOf(dateS);
      newValue.push({
        label: data.questions[question],
        value: data.values[question][country][date]
      })
  }
  console.log(newValue)
})


var data =
[
    [
        {label: "Large Choice of Food", value: 0},
        {label: "Food Quality", value: 0},
        {label: "Food freshness", value: 0},
        {label: "Taste of food", value: 0},
        {label: "Waiting time to recieve food", value: 0},
        {label: "Value for money", value: 0}
    ],
    [
        {label: "Large Choice of Food", value: 0},
        {label: "Food Quality", value: 0},
        {label: "Food freshness", value: 0},
        {label: "Taste of food", value: 0},
        {label: "Waiting time to recieve food", value: 0},
        {label: "Value for money", value: 0}
    ],
    [
        {label: "Large Choice of Food", value: 0},
        {label: "Food Quality", value: 0},
        {label: "Food freshness", value: 0},
        {label: "Taste of food", value: 0},
        {label: "Waiting time to recieve food", value: 0},
        {label: "Value for money", value: 0}
    ]
];

console.log(data)

json data;

{
    "dates": ["Jan_2016", "Feb_2016", "Mar_2016"],
    "questions": ["Large choice of food", "Food quality", "Food freshness", "Taste of food", "Waiting time to recieve food", "Value for money"],
    "countries": ["Netherlands", "Belgium", "France"],
    "values": [
        [
            [5, 88, 18],
            [50, 83, 10],
            [29, 78, 80]

        ],

        [
            [46, 51, 61],
            [95, 21, 15],
            [49, 86, 43]

        ],
        [
            [7, 46, 92],
            [54, 94, 31],
            [89, 96, 11]

        ],
        [
            [71, 56, 54],
            [12, 45, 3],
            [67, 73, 92]

        ],
        [

            [28, 89, 97],
            [15, 66, 91],
            [19, 89, 72]
        ],
        [
            [54, 15, 61],
            [83, 61, 9],
            [10, 96, 57]
        ]
    ]
}

I've been banging my ahead against a wall trying to figure this out, hopefully the question makes sense as I'm not sure I've explained it well enough?

Anyway, please let me know if you'd like me to try explain it further, and any advice is much appreciated!

Upvotes: 2

Views: 79

Answers (2)

andre mcgruder
andre mcgruder

Reputation: 1520

According to what you said you are tying to achieve this would be a better format for your data.

You have an array of date object that each contain an array of countries and questions. The questions array contains the question with an array of answers. This way a data can have a varied number of countries and questions. The questions can have a varied number of answers.

var data = [
    {
        'date': 'Jan_2016',
        'countries': ['Netherlands', 'Belgium', 'France'],
        'questions': [
                         {'Large choice of food': [5, 88, 18]},
                         {'Food quality': [46, 51, 61]},
                         {'Food freshness': [7, 46, 92. 55, 87]},
                         {'Taste of food': [71, 56, 54]},
                         {'Waiting time to recieve food': [28, 89, 97]},
                         {'Value for money': [54, 15, 44, 61]},
                     ]
    },
    {
        'date': 'Feb_2016',
        'countries': ['Netherlands', 'Belgium', 'France', 'Japan', 'Africa', 'Germany'],
        'questions': [
                         {'Large choice of food': [50, 83, 10]},
                         {'Food quality': [95, 21, 15]},
                         {'Food freshness': [54, 94, 31]},
                         {'Taste of food': [71, 56, 54, 14]},
                         {'Waiting time to recieve food': [15, 66, 91]},
                         {'Value for money': [83, 61, 9]},
                     ]
    }
];

Upvotes: 0

Midas
Midas

Reputation: 7131

If I understand your question correctly, you are looking for this:

d3.json("http://run.plnkr.co/JMKRNs0G3MYBqSnI/data.json", function(error, data) {

  var newValue = [];

  var dateS = "Feb_2016";
  var k = data.dates.indexOf(dateS);

  for (var i = 0; i < data.countries.length; i++) {
    var country = data.countries[i];
    var answers = [];
    for (var j = 0; j < data.questions.length; j++) {
      answers.push({
        label: data.questions[j],
        value: data.values[j][i][k]
      });
    }
    newValue.push(answers);
  }
  console.log(newValue);
});
<script data-require="d3@*" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>

Upvotes: 1

Related Questions