0

I receive data from a table. And save them in array.

I am wondering how to convert array into string with the following format.

In my request,users can add new rows and type in questions and answers.

That's why I think I can put all data in the "form:input"

My code:

var param = [];
$("#tblPets tbody tr").each(function(index){

 param.push({
    question: $("textarea.question", this).val(),
    answer: $("textarea.answer", this).val(),
 });

});
var json = JSON.stringify(param); 

    $("form #queAns").val(json);

I can convert this array into JSON string.but the format is not what im looking for.

For example:

I hope my string can look like:

Question,Answer|Question,Answer|Question,Answer|

How to do this??

user2959107
  • 1
  • 1
  • 1
  • 2
  • 5
    Why are you making up your own serialization rather than using a standard, well-supported one such as JSON? – Matt Ball Nov 06 '13 at 05:53
  • Look at jQuery `Map()` – jasonscript Nov 06 '13 at 05:53
  • 2
    Why not directly create a string with the desired format instead of first looping and pushing records into the array? – Ankit Jaiswal Nov 06 '13 at 05:55
  • Try [Array.prototype.toString](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.2) or [JSON.stringify(array)](http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.3) or even [Array.prototype.join](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.5). – RobG Nov 06 '13 at 06:09
  • Why is this on hold as off-topic? He has an attempted solution, and he obviously doesn't want to use the standard serialization. I don't see why this is marked as off-topic. @Matt Ball, Please elaborate. – AndrewK Nov 06 '13 at 16:14
  • @AndrewK because the OP did not provide evidence of an effort to solve the problem on their own, as per the stated close details below. As it says: _"Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results."_ The OP has not demonstrated a minimal understanding of the problem being solved; consequently, the question is off topic. – Matt Ball Nov 06 '13 at 16:31
  • @Matt Ball, he is asking for help/advice for how to solve this problem that he doesn't know how to solve. He attempted to use JSON.stringify but that did not pan out, and he did not know where to go from there. I disagree, but obviously others agree that this is off-topic so I will concede to that. – AndrewK Nov 06 '13 at 17:26

3 Answers3

1

Try using JQuery's $.map() function, and inside that looping over the properties on the object.

var param = [];
$("#tblPets tbody tr").each(function(index){

 param.push({
    question: $("textarea.question", this).val(),
    answer: $("textarea.answer", this).val(),
 });

});

console.log(param);

var result = '';
$.map(param, function(n) {
    for(var prop in n){
        result += n[prop] + ',';
    }
    result = result.substring(0, result.length-1) + '|';
});

console.log(result);

jsFiddle - updated to remove extra comma

StackOverflow re: iterating over object properties

Community
  • 1
  • 1
Brian Oliver
  • 1,407
  • 2
  • 15
  • 25
  • Thanks!! I use your code and it works good.I got what I want.but there is a little issue. The Result looks like:Question,Answer,|Question,Answer,| I dont wanna have "," after answer. So how to do it? – user2959107 Nov 06 '13 at 06:30
  • Glad it worked for you! I've updated the code and the jsFiddle to remove that extra space. – Brian Oliver Nov 06 '13 at 13:41
0

There are too many method exist to convert array into string:

You can use .tostring():

myArray .toString();

Try this:

Javascript:

function myFunction()
{
  var myArray = ["Banana", "Orange", "Apple", "Mango"];
  myArray .toString();
  var x=document.getElementById("demo");
  x.innerHTML=myArray;
}

HTML:

<p id="demo">Click the button to convert the array into a String.</p>

Live Example

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
0

I agree with the comments, but here is an idea of how it would look like if you want the exact encoding and are ok with looping over the array.

var param = [], string = '';
$("#tblPets tbody tr").each(function(index){

 param.push({
    question: $("textarea.question", this).val(),
    answer: $("textarea.answer", this).val(),
 });

});

param.forEach( function (obj) { 
string += obj.question+','obj.answer+'|';
})
Abraham Adam
  • 635
  • 1
  • 6
  • 16