当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Reformat data from 2 arrays and include a count

问题描述:

I have 2 arrays one called question and another called answer. I would like to consider the element in question as a top category. and answer as the sub category. so each question can have different answers. The arrays match up. I need to reformat the data as shown below.

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

Example output:

{

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

]

}

网友答案:

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);
网友答案:

The exact structure you want in your code is somewhat weird, I'm giving you the bare basic code you need to zip and aggregate the answers, afterwards if you really want you can modify the output to match your needs.

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)
网友答案:

You could use a nested hash table approach for referencing the inner items of the result array.

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
网友答案:

Here is a DEMO

i have a fanny time with this, thanks

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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: