将Array对象转换为字符串jquery

时间:2022-05-08 20:22:00

I am posting a form via ajax and want to send the form data like string . Below is my code .

我通过ajax发布表单,并希望像字符串一样发送表单数据。以下是我的代码。

var formdata=$(this).serializeArray();
var objList = [];
for (var i = 1; i <= $("input[name=Range]").val(); i++) {

tempObj = {};

$.each(formData, function (key, value) {


                        if (value.name.startsWith("member"){

                        }
                        else {
                            tempObj[value.name] = value.value;
                        }

                    });


                    tempObj["member"] = $("input[name=member"+i+"]").val();

                    tempObj["Range"] = 1;




                    objList.push(tempObj);

                }

                console.log(objList);

If Range = '2' I get 2 Array Object in console like this:

如果Range ='2',我在控制台中得到2个Array对象,如下所示:

Name:"John"  
Department:"Training"  
Areacode:"23"
Member:"2"



Name:"Sam" 
Department:"HR"
Member:"2"
Areacode:"13"

But I want to post is data as a Form Url like:

但我想将数据发布为Form Url,如:

"Name=John&Department=Training&Member=2&Areacode=23"   



  "Name=Sam&Department=HR&Member=1&Areacode=13" 

What can I do in code ?

我可以在代码中做什么?

2 个解决方案

#1


2  

I will continue your code insteed of modifying what you have.

我将继续修改你所拥有的代码。

We start from objList. And we will map this array to create a new array but insteed of an object array it will be a string array.

我们从objList开始。我们将映射此数组以创建一个新数组,但是对于一个对象数组,它将是一个字符串数组。

You can then add the reduce method to iterate over the object and crete you string no matter how many values it has.

然后,您可以添加reduce方法来迭代对象,并创建字符串,无论它有多少值。

var formdata = $(this).serializeArray();
var objList = [{
Name:"John", 
Department:"Training"  ,
Areacode:"23",
Member:"2"},{
Name:"Sam" ,
Department:"HR",
Member:"2",
Areacode:"13",
Extra:"value"}];

let arrStr = objList.map(obj => {
  return Object.entries(obj).reduce( (key, val,i) => `${key}${i>0?'&':''}${val[0]}=${val[1]}`, "");
})

console.log("This is the array of strings:"+arrStr);
console.log("String 1:"+arrStr[0]);
console.log("String 2:"+arrStr[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

"Name=John&Department=Training&Member=2&Areacode=23"

Hope this helps :)

希望这可以帮助 :)

#2


2  

From the tempObj you already have:

从tempObj你已经有:

const tempObj = {
  Name:"John"  ,
  Department:"Training"  ,
  Areacode:"23",
  Member:"2"
}

const strObj=Object.entries(tempObj).reduce( (str, entry,i) => `${str}${i>0?'&':''}${entry[0]}=${entry[1]}`, "")
    
console.log(strObj)

#1


2  

I will continue your code insteed of modifying what you have.

我将继续修改你所拥有的代码。

We start from objList. And we will map this array to create a new array but insteed of an object array it will be a string array.

我们从objList开始。我们将映射此数组以创建一个新数组,但是对于一个对象数组,它将是一个字符串数组。

You can then add the reduce method to iterate over the object and crete you string no matter how many values it has.

然后,您可以添加reduce方法来迭代对象,并创建字符串,无论它有多少值。

var formdata = $(this).serializeArray();
var objList = [{
Name:"John", 
Department:"Training"  ,
Areacode:"23",
Member:"2"},{
Name:"Sam" ,
Department:"HR",
Member:"2",
Areacode:"13",
Extra:"value"}];

let arrStr = objList.map(obj => {
  return Object.entries(obj).reduce( (key, val,i) => `${key}${i>0?'&':''}${val[0]}=${val[1]}`, "");
})

console.log("This is the array of strings:"+arrStr);
console.log("String 1:"+arrStr[0]);
console.log("String 2:"+arrStr[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

"Name=John&Department=Training&Member=2&Areacode=23"

Hope this helps :)

希望这可以帮助 :)

#2


2  

From the tempObj you already have:

从tempObj你已经有:

const tempObj = {
  Name:"John"  ,
  Department:"Training"  ,
  Areacode:"23",
  Member:"2"
}

const strObj=Object.entries(tempObj).reduce( (str, entry,i) => `${str}${i>0?'&':''}${entry[0]}=${entry[1]}`, "")
    
console.log(strObj)