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 ?

1
  • Is the array something like this: {Name:"Sam" , Department:"HR", Member:"2", Areacode:"13"} Commented May 4, 2018 at 16:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
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. :)
please dont forget to upvote @TiagoCoelho answer. And thank for the upvote jajaja
Hello again above code make one string , which Contains both of them , is there any possible was so that i can get 2 strings ?
I wanted it like 1{"Name=John&Department=Training&Member=2&Areacode=23" } 2{"Name=Sam&Department=HR&Member=1&Areacode=13" }
|
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)

1 Comment

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"]

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.