Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:06:32+00:00 2026-06-09T12:06:32+00:00

I have some input fields than I want as an array so that I

  • 0

I have some input fields than I want as an array so that I can view them in a table on my page. I have been able to create an array using .serializeArray()

In this fiddle I have been able to output my array however I want the data to appear as it does in the table I have hard coded at the bottom of the fiddle, where it groups all the instances of Tom and Jerry into one row per id. Accumalating all sales values for that id etc. And I want to sort it by the total sale price column. I am aware of server side techniques to do this however in this case I am looking for a jquery solution. What is the best way to acheive this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:06:33+00:00Added an answer on June 9, 2026 at 12:06 pm

    I’m assuming you can rely on the hidden input fields always appearing in groups of four with an id[], name[], sales[] & price[] for each group, otherwise (obviously) you can’t tell which fields are related. So rather than use .serializeArray(), which returns a single array with all the values, I’d put the ids in their own array, the names in their own, and so forth. Perhaps something like this:

    function showValues() {
        function getVal(el, i) {
            return el.value;
        }
        var ids = $.map($('input[name="id[]"]'), getVal),
            names = $.map($('input[name="name[]"]'), getVal),
            sales = $.map($('input[name="sales[]"]'), getVal),
            prices = $.map($('input[name="price[]"]'), getVal),
            data = {},
            i,
            $results = $("#results");
    
        for (i = 0; i < ids.length; i++) {
            if (!data[ids[i]]) {
                // if current id is new add a record for it:
                data[ids[i]] = {
                    "id":ids[i],"name":names[i],"sales":+sales[i],"price":+prices[i]
                };
            } else {
                // otherwise add to existing record's totals
                data[ids[i]].sales += +sales[i];
                data[ids[i]].price += +prices[i];
            }
        }
        // data object now contains the details for each salesman,
        // so turn it into an array to allow sorting:
        data = $.map(data, function(val, key) { return val; });
        data.sort(function(a,b) { return a.price - b.price; });
    
        // now output table - assume there's already a table element with headings
        $.each(data, function(i, val) {
            var $tr = $("<tr></tr>").attr("data-id", val.id).appendTo($results);
            $("<td></td>").text(val.name).appendTo($tr);
            $("<td></td>").text(val.sales).appendTo($tr);
            $("<td></td>").text(val.price).appendTo($tr);
            $("<td></td>").text(val.price / 10).appendTo($tr);
        });
    }
    

    Working demo: http://jsfiddle.net/nnnnnn/VNSam/5/

    By way of explanation, this line:

    ids = $.map($('input[name="id[]"]'), getVal)
    

    …says to get all inputs with name="id[]", and pass the resulting jQuery object to the $.map() method so that we can get back an array containing just the id values – you can see that getVal() is just a simple function that does exactly that. I do the same thing for name[] and the other fields.

    Note also that the sales and price values are strings when retrieved from the inputs, so I’m using the unary plus operator to convert them to numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have one label that is associated with the input field. Some
I have a fieldset in which I have some input fields and a checkbox.
I have a form with some input fields. I am using the tabStrip KendoUI
I have a form with some input fields and a little <div> to the
I have am attempting to add some input fields to a jquery ui dialog
I am using ASP.NET2.0. I have created a download form with some input fields
I have a form with some generated input fields like theese: <input value=March name=month[March]
I have a drop down box and some text input fields below it. Based
I have some input fields like below: <input type=text name=date_1 class=dataList value=2009-12-30 /> <input
For instance, I want to have a html form that looks like this: <table>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.