从2个阵列重新格式化数据并包含计数

时间:2017-02-08 21:36:15

标签: javascript arrays object

我有2个数组,一个叫做问题,另一个叫做答案。我想将有问题的元素视为最高类别。和answer作为子类别。所以每个问题可以有不同的答案。阵列匹配。我需要重新格式化数据,如下所示。

var question = 
    [ 'Prompteness',
      'Prompteness',
      'Prompteness',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable']
  var answer =
    [ 'On hold too many times',
      'On hold too many times',
      "Lots of silence",
      'Still a little confused',
      'Still a little confused',
      'Still a little confused',
      "They knew nothing" ]

示例输出:

{
    name : "Prompteness",
    value : [{
        name : 'On hold too many times',
        data : 2
    },{
        name : "Lots of silence",
        data : 1
    }]
},{
    name : 'Knowledgeable',
    value : [{
        name : 'Still a little confused',
        data : 3
    },
        name : "They knew nothing",
        data : 1
    ]
}

4 个答案:

答案 0 :(得分:1)



var question = [
  'Prompteness',
  'Prompteness',
  'Prompteness',
  'Knowledgeable',
  'Knowledgeable',
  'Knowledgeable',
  'Knowledgeable'
]
var answer = [
  'On hold too many times',
  'On hold too many times',
  "Lots of silence",
  'Still a little confused',
  'Still a little confused',
  'Still a little confused',
  "They knew nothing"
]

var result = [];
question.forEach((q, i) => { // for each item in question (and answer)
  var o = result.find(e => e.name == q); // look for the question inside result
  
  if(!o) { // if it does not exist, add it
    o = {name: q, value: []};
    result.push(o);
  }
  
  var found = o.value.find(e => e.name == answer[i]); // look for the answer in the value array of this question
  if(found) // if we found the answer 
    found.data++; // increment its data property
  else // if not
    o.value.push({name: answer[i], data: 1}); // add it to the value array of this question with a count of 1
});

console.log(result);




答案 1 :(得分:0)

您在代码中想要的确切结构有点奇怪,我为您提供了压缩和汇总答案所需的基本代码,之后如果您真的希望可以修改输出以满足您的需求。



var question =
    [ 'Prompteness',
      'Prompteness',
      'Prompteness',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable']

var answer =
  [ 'On hold too many times',
    'On hold too many times',
    "Lots of silence",
    'Still a little confused',
    'Still a little confused',
    'Still a little confused',
    "They knew nothing" ]

var result = {}

for(var i = 0; i < answer.length; i++) {
  var q = question[i];
  var a = answer[i];

  if(!result[q]) {
    result[q] = {};
  }

  if(!result[q][a]) {
    result[q][a] = 1
  } else {
    result[q][a] = result[q][a] + 1
  }

}

console.log(result)
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用嵌套哈希表方法来引用结果数组的内部项。

&#13;
&#13;
var question = ['Prompteness', 'Prompteness', 'Prompteness', 'Knowledgeable', 'Knowledgeable', 'Knowledgeable', 'Knowledgeable'],
    answer = ['On hold too many times', 'On hold too many times', "Lots of silence", 'Still a little confused', 'Still a little confused', 'Still a little confused', "They knew nothing"],
    result = [];

question.forEach(function (o) {
    return function (a, i) {
        var r = o;
        if (!r[a]) {
            r[a] = { _: [] };
            r._.push({ name: a, value: r[a]._ });
        }
        r = r[a];
        if (!r[answer[i]]) {
            r[answer[i]] = { name: answer[i], data: 0 };
            r._.push(r[answer[i]]);
        }
        r[answer[i]].data++;
    };
}({ _: result }));
    
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这是DEMO

我有一个很好的时间,谢谢

var question = 
    [ 'Prompteness',
      'Prompteness',
      'Prompteness',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable',
      'Knowledgeable']
  var answer =
    [ 'On hold too many times',
      'On hold too many times',
      "Lots of silence",
      'Still a little confused',
      'Still a little confused',
      'Still a little confused',
      "They knew nothing" ]
      var str =question[0];
      var str1 = '';
      var p = {
            name: str,
            value: []
          }

      var _generate = [p];
      var g = 0;
      var g1 = 0;
      for(var i =0; i<question.length;i++){

        if(str!=question[i]) {
        console.log('\t',str,' -- ');
          str=question[i];
          _generate.push({
            name: str,
            value: []
          });
            g1=0;
          g++;
        }
        if(str1!=answer[i]){
        str1=answer[i];
        console.log('\t\t',answer[i],' -- ',answer.indexOf(answer[i]));


          var obj = {
            name:str1,
            data:0
          }
          str1=answer[i];
          g1++;
            _generate[g].value.push(obj)
        }
        console.log(g, g1);
        _generate[(g)].value[(g1-1)].data++;

      }

      console.log(_generate)