2017-02-08 5 views
0

私は2つの配列を質問と呼び、もう1つは答えと呼んでいます。問題の要素をトップカテゴリと見なしたいと思います。サブカテゴリはanswerです。それぞれの質問は異なる答えを持つことができます。配列が一致します。私は以下のようにデータを再フォーマットする必要があります。2つの配列のデータを再フォーマットし、カウントを含む

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 
    ] 
} 
+0

は、あなたが自分でそれを実行しようとしましたがありますか?あなたがした場合は、あなたのコードを投稿してください – gauss

答えて

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);

+0

あなたの答えをありがとう。あなたはニーナ・シュルツの答えのようなデータを作ることができますか?それは私が望む結果の多くです。 –

+0

私が望む結果は、オブジェクトの配列です。 { "Prompteness":{ "name": "Prompteness"、 "私はそれを持っていません。 –

+1

@jackblank更新済み! –

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)

0

あなたは結果の配列の内側の項目を参照するために、ネストされたハッシュテーブルのアプローチを使用することができます。ここで

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

+0

データは私が欲しいものですが、これは少し複雑です。オブジェクト '{_:result} 'を使って' forEach'を自分自身で呼び出していますか?それぞれのforの最初のパラメータは質問配列の最初の要素だと思っていました。そして、私は "ネストされたハッシュテーブルアプローチ"があなたがJavaScriptを使って簡単な例を示しているかどうかわかりません。多分私がオンラインで読むことができるもの。私は、ハッシュテーブルはJavaScript内のオブジェクトリテラルを意味すると考えます。 –

+0

'function(o){...}({_:result})'は結果を参照するハッシュテーブルのクロージャです。 –

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) 
関連する問題