javascript 如何使用jquery将数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19805062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:51:48  来源:igfitidea点击:

How to convert array into string using jquery

javascriptjqueryarrays

提问by user2959107

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.

我可以将此数组转换为 JSON 字符串。但格式不是我要找的。

For example:

例如:

I hope my string can look like:

我希望我的字符串看起来像:

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

问题,答案|问题,答案|问题,答案|

How to do this??

这个怎么做??

回答by Brian Oliver

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

尝试使用 JQuery 的$.map()函数,并在该函数内部循环对象上的属性。

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

jsFiddle - 更新以删除额外的逗号

StackOverflow re: iterating over object properties

StackOverflow re:迭代对象属性

回答by Ishan Jain

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

将数组转换为字符串的方法太多了:

You can use .tostring():

您可以使用.tostring()

myArray .toString();

Try this:

试试这个:

Javascript:

Javascript:

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

HTML:

HTML:

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

Live Example

现场示例

回答by Abraham Adam

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+'|';
})