1

基本的に、単純な文字列に組み込む必要がある多次元配列があります。

非常に簡単な質問ですが、私はそれを釘付けにすることができないので、かなり長い間私を悩ませてきました.

以下は、配列内に 3 つの質問のみを配置した場合の例です。

[["question1","answer1","answer2","answer3","answer4"],["question2","answer1","answer2","answer3","answer4"],["question3","answer1","answer2","answer3","answer4"]]

たとえば、明確にするために、「 D.rows[0][0]question1」とD.rows[2][3]「answer3」となります。

次のように文字列に保存する必要があります。

question1,answer1,answer2,answer3,answer4 
question2,answer1,answer2,answer3,answer4 
question3,answer1,answer2,answer3,answer4 

各要素の間にはコンマが必要であり、各質問は改行で区切る必要があります。

これは私が現在持っているもので、機能していません:

var fullString;
for (i = 0; i < csvArray.length; ++i) 
{
    second = secondArray[i];
    for (j = 0; j < second.length; ++j) 
    {
        fullString += entry[j] + "'";
    }
    fullString += "\n";
}

前もって感謝します!

4

6 に答える 6

1

これを試して

var s,a=[["question1","answer1","answer2","answer3","answer4"],"question2","answer1","answer2","answer3","answer4"],["question3","answer1","answer2","answer3","answer4"]];
for(i=0; i < a.length; i++){
    s=(s)?s+"\n\r"+a[i].join():a[i].join();
}

jsfiddle の例

あなた自身の例では、 に直接追加するfullStringので、値には空の文字列が必要です。そうしないとundefined、最初に になります。

var fullString="";

この部分second = secondArray[i];はあるべきだった

var second = csvArray[i];

そして同じように、これfullString += entry[j] + "'";はそうあるべきだった

fullString += second[j] + ",";
于 2013-08-24T06:09:08.180 に答える
1

一発ギャグ:

var result = [["question1","answer1","answer2","answer3","answer4"],["question2","answer1","answer2","answer3","answer4"],["question3","answer1","answer2","answer3","answer4"]].join('\r\n');

于 2013-08-24T06:13:07.973 に答える
0

このようなもの:

var questionAnswerSet= { "question1" : [
{ "answer1" : "value", "answer2" : "value", "answer3" : value} ],
"question2" : [
{ "answer1" : "value", "answer2" " : "値", "answer3" : 値} ], }

次のようにアクセスします。

questionAnswerSet[0].answer2 // 質問 1 回答 2

questionAnswerSet[1].answer2 // 質問 2 回答 2

于 2013-08-24T06:02:12.610 に答える
0
for (var i=0; i<yourArray.length; i++) { // iterate on the array
   var obj = yourArray[i];
   for (var key in obj) { // iterate on object properties
      var value = obj[key];
      console.log(value);
   }
}
于 2013-08-24T06:10:03.207 に答える
0

区切り記号や回答数を変更する予定がある場合は、これが解決策になる可能性があります。

var array = [["question1","answer1","answer2","answer3","answer4"],["question2","answer1","answer2","answer3","answer4"],["question3","answer1","answer2","answer3","answer4"]],
string = "";

for (var i = 0; i <= array.length - 1; i++) {
     string +=array[i].join(', ');
     string += "\n";
}

また、この構造のオブジェクトに対して、読みにくいが効果的な方法でも

string = array.join('\r\n');
于 2013-08-24T06:17:56.523 に答える
0

このような配列がある場合...

var arr = [["question1","answer1","answer2","answer3","answer4"],["question2","answer1","answer2","answer3","answer4"],["question3","answer1","answer2","answer3","answer4"]];

それから...

var strArr  =  arr.join(',');
var strLine =  arr.join('\n'); // string with line breaks.

あなたのためにそれをします。

質問と回答のブロックごとに異なる文字列が必要な場合は...

var strJoinArr = [], strJoinLines = '';
for(var i = 0; i < arr.length; i++){
    strJoinArr.push(arr[i].join(','));
    strJoinLines += arr[i].join(',')+ '\n'; // string with line break
}

次に、各セクションにアクセスするには、インデックスを使用できます。

たとえばstrJoinArr[2]'question3,answer1,answer2,answer3,answer4'

もっともっと.join() もっと.push()

于 2013-08-24T06:07:48.833 に答える