基于一个属性javascript

时间:2016-06-14 15:43:25

标签: javascript arrays

我试图循环遍历一个多维数组,首先根据月份检索一组值,然后在那之后创建另一个按问题排列的数组和与之关联的值。

我意识到这没有任何意义,所以这里有一个plnk;

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

如果你检查控制台,var data的第一个控制台输出是我想要实现的结构(父数组代表日期,代表数据的6个数组)。

var newValue目前正在编入索引,我想创建一个包含此输出的数组,但是对于数据中的每个日期。

这是代码;

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数据;

{
    "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]
        ]
    ]
}

我一直在撞墙试图解决这个问题,希望这个问题有道理,因为我不确定我已经解释得够好了吗?

无论如何,如果您想让我尝试进一步解释,请告诉我,我们非常感谢您的建议!

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你正在寻找:

&#13;
&#13;
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);
});
&#13;
<script data-require="d3@*" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据您所说的,您要实现这一目标,这将是一种更好的数据格式。

您有一个日期对象数组,每个数组都包含一系列国家/地区和问题。问题数组包含一系列答案的问题。这样,数据可以包含多个国家和问题。问题可以有不同的答案。

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]},
                     ]
    }
];