2

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

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:

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:

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



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

What can I do in code ?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Pankash Mann
  • 59
  • 3
  • 12

2 Answers2

3

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.

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

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 :)

Gerardo BLANCO
  • 5,590
  • 1
  • 16
  • 35
  • For reference I have given 4 values i.e Name , Department, Area Code , Member but in form i have 30 values in total which i need to post , is their any one line code ? Or simply serialize this thing ? – Pankash Mann May 04 '18 at 17:05
  • 1
    I edited the answer. This code will take care no matter how many values or objects you have. You can see i added an extra value to the second object. I combined @TiagoCoelho with my answer to complete this code. If this works please upvote his answer too. :) – Gerardo BLANCO May 04 '18 at 17:20
  • please dont forget to upvote @TiagoCoelho answer. And thank for the upvote jajaja – Gerardo BLANCO May 04 '18 at 17:45
  • Hello again above code make one string , which Contains both of them , is there any possible was so that i can get 2 strings ? – Pankash Mann May 05 '18 at 14:16
  • I wanted it like 1{"Name=John&Department=Training&Member=2&Areacode=23" } 2{"Name=Sam&Department=HR&Member=1&Areacode=13" } – Pankash Mann May 05 '18 at 14:18
  • The output is a string array. I edited so you can see you have 2 separate strings – Gerardo BLANCO May 05 '18 at 15:26
2

From the tempObj you already have:

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)
Tiago Coelho
  • 5,023
  • 10
  • 17
  • It works if i have to send 1 string , if i have multiple request i combines everthing in one string like ["Name=John &Department=Training&Areacode23","Name=Sam&Department=HR&AreaCode=23"] – Pankash Mann May 04 '18 at 17:22