1

Basically, I have a multidimensional array that I need to build into a simple string.

Quite an easy question, although it has been eating away at me for quite some time now since I can't seem to nail it.

Here is an example of how my array could look with just 3 questions within it:

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

For example, D.rows[0][0] would be "question1" and D.rows[2][3] would be "answer3", just to clarify.

Here is how it must be saved into a string as:

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

Each element must have a comma between them, and each question must be separated by a line-break.

This is what I currently have that is not working:

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

Thanks in advance!

6 Answers 6

1

try this

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 example

In your own example: since you are going straight to adding to fullString, it should have an empty string for value, otherwise you will end up with undefined in the beginning.

var fullString="";

this part second = secondArray[i]; should have been

var second = csvArray[i];

and in a same way this fullString += entry[j] + "'"; should have been

fullString += second[j] + ",";
Sign up to request clarification or add additional context in comments.

2 Comments

@fizzix - in for in loop order of iteration is not specified in ecma script spec - results may vary in different browsers, also if you add to array prototype in ES3, you will get that too - use for(i=0; i < arr.length; i+=1)
thanks @magyar1984 for the notice, been on ES5 for a while now and tend to forget some of the differences ^^. But personally I would even recommend going with the answer given by spy-js
1

one liner:

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

Comments

0

something like this:

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

and access like this:

questionAnswerSet[0].answer2 // question one answer 2

questionAnswerSet[1].answer2 // question two answer 2

2 Comments

Sorry, but that makes no sense at all? I believe that I need a double for loop to go over every element within my array.
My array can have any amount of entries? And the elements will always be different...
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);
   }
}

Comments

0

If you have an array as this...

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

then...

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

will do that for you.

if you want different strings for each question-answer block, then...

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
}

then to access each section you can use indexes,

For example, strJoinArr[2] will return 'question3,answer1,answer2,answer3,answer4'

more on .join() more on .push()

1 Comment

How does that add the line breaks?
0

This might be your solution if you plan ot change separators or number of answers.

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

Also, in the less readable but also effective way, for any object of this structure

string = array.join('\r\n');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.