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

  • Home
  • SEARCH
  • 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 6051847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:51:46+00:00 2026-05-23T07:51:46+00:00

I need to serialize an associative JavaScript array. It’s a simple form of products

  • 0

I need to serialize an associative JavaScript array.
It’s a simple form of products and a numeric values, but just after building the array seems empty.

The code is here: http://jsbin.com/usupi6/4/edit

  • 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-05-23T07:51:46+00:00Added an answer on May 23, 2026 at 7:51 am

    In general, don’t use JS arrays for “associative arrays”. Use plain objects:

    var array_products = {};
    

    That is why $.each does not work: jQuery recognizes that you pass an array and is only iterating over numerical properties. All others will be ignored.

    An array is supposed to have only entries with numerical keys. You can assign string keys, but a lot of functions will not take them into account.


    Better:

    As you use jQuery, you can use jQuery.param [docs] for serialization. You just have to construct the proper input array:

    var array_products = []; // now we need an array again
    $( '.check_product:checked' ).each(function( i, obj ) {
        // index and value
        var num = $(obj).next().val();
        var label = $(obj).next().next().attr( 'data-label' );
        // build array
        if( ( num > 0 ) && ( typeof num !== undefined ) ) {
            array_products.push({name: label, value: num});
        }      
    });
    
    var serialized_products = $.param(array_products);
    

    No need to implement your own URI encoding function.

    DEMO


    Best:

    If you give the input fields a proper name:

    <input name="sky_blue" class="percent_product" type="text" value="20" />
    

    you can even make use of .serialize() [docs] and greatly reduce the amount of code (I use the next adjacent selector [docs]):

    var serialized_products = $('.check_product:checked + input').serialize();
    

    (it will include 0 values though).

    DEMO

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

Sidebar

Related Questions

I have a simple array that I need to serialize as part of a
I need to serialize/de-serialize some objects into/from string and transfer them as just opaque
I have a simple Java class that I need to serialize to be stored
I need tp serialize a group of input elements but I can't for the
I need to serialize a matched set so it's values can be passed to
I need to store a multi-dimensional associative array of data in a flat file
I need to serialize all inputs from a form into a JSON string. With
I need to serialize a collection, but I would like to know if there
I need to serialize a tree of simple objects as XML to/from a file
I need to serialize array which contains URLs: Array( 'url1' => 'http://www.example.com', 'url2' =>

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.