How can you map the differences between Javascript objects?

How to retrieve multidimensional form inputs as a javascript array of objects?

  • I am working on a Backbone app that requires the input of rows of data (read: an array of objects). I have the form set out like so: <tr> <td><input type="text" name="items[][qty]"></td> <td><input type="text" name="items[][job_number]"></td> <td><input type="text" name="items[][description]"></td> <td><input type="text" name="items[][purchase_order]"></td> </tr> <!--...--> with a dynamic number of rows. I want to be able to retrieve the data in the form of: { items: [ { qty: [val], job_number: [val], description: [val], purchase_order: [val] }, // [...] ] } The closest solution I have found is by Aaron Shafovaloff (http://aaronshaf.wordpress.com/code/jquery-plugin-serialize-multi-dimensional-form-data-into-json/) but it doesn't support the arrays in the output (only objects). I could modify his code to do what I need but I figured I would ask here first since there is no point reinventing the wheel. I am using jQuery and Underscore in my project so have access to their methods.

  • Answer:

    You don't really need a plugin or library, it is pretty trivial to write yourself. var formData = new FormData(someFormElement); for(var pair of formData.entries()) {     console.log(pair[0]+ ', '+ pair[1]); } You basically just need to parse your key (aka name) and create the appropriate object and insert into your array. https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects https://developer.mozilla.org/en/docs/Web/API/FormData

William Teo at Quora Visit the source

Was this solution helpful to you?

Other answers

Firstly, that seems like a lot of data to send via a single POST request you might have to do it in chunks (i.e if you are going to send it to a server eventually). You could try converting the input data to JSON format and then parse it to get your Javascript object . Here is how you can convert your form data to JSON: http://stackoverflow.com/a/13038218 it has a JS and a jQuery method. You could then use jsObject = JSON.parse(jsonString) to get your JS object (here is some documentation on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).

Shasak Raina

The jQuery 'map' function is your best friend here!First grab the values you need via regular ol' jQuery: $('.someFormInputs')Then map them to an object you define: var allthedata = $('.someFormInputs').map(function() { var input = { }; input.someId = $(this).text(); input.name = $(this).text(); // Etc }).get(); .get() returns "allthedata" as an array of json objectsHope this helps!** You may need to use $(this).children().text()

Arbab Khan

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.