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 7997463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:58:15+00:00 2026-06-04T14:58:15+00:00

I got the following function, that loops through all table th s and builds

  • 0

I got the following function, that loops through all table ths and builds an array based on the selectors values.

The output is a numeric array, containing sub objects as values.

Now .get() allows me to build the array, but I can’t find out how to set this to an associative array.

Question: How would I add the first table cell value as assoc array index?

( function( $ ) 
{
    $.fn.loadTableData = function( selectors )
    {
        var id = $( this ).attr( 'id' );

        /**
         * Get the output array keys from the <thead/tfoot> cell/th #ids
         */
        var columns = $( '#' + id + ' table th' ).map( function($) 
        {
            // This assumes that your headings are suitable to be used as
            // JavaScript object keys. If the headings contain characters 
            // that would be invalid, such as spaces or dashes, you should
            // use a regex here to strip those characters out.
            return jQuery( this ).attr( 'id' );
        } );

        /**
         * Get the cell data, based on the selector object values by index
         * @return row 
         */
        var cell_data = $( '#' + id + ' table tr' ).map( function( index )
        {
            var row = {};

            // Skip empty rows (example: <thead/tfoot>)
            if ( 0 === $( this ).find( 'td' ).length )
                return;

            // Find all of the table cells on this row.
            $( this ).find( 'td' ).each( function( index, value ) 
            {
                // Skip empty keys
                if ( undefined == selectors[ index ] )
                    return;

                // Determine the cell's column name by comparing its index
                // within the row with the columns list we built previously.
                var row_name = columns[ index ];

                // Add a new property to the row object, using this cell's
                // column name as the key and the cell's text as the value.
                if ( 'text' === selectors[ index ] )
                {
                    row[ row_name ] = $( this ).text();
                }
                else
                {
                    row[ row_name ] = $( this ).find( selectors[ index ] ).val();
                }
            } );

            // Finally, return the row's object representation, to be included
            //  in the array that $.map() ultimately returns.
            return row;

            // Don't forget .get() to convert the $ set to a regular array.
        } ).get();

        return cell_data;
    };
} )( jQuery );

Thanks!

  • 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-04T14:58:17+00:00Added an answer on June 4, 2026 at 2:58 pm

    The solution wasn’t that hard at the end: Used .each() instead of .map() and then assigned to the output object[ final_index ] instead of using .get().

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

Sidebar

Related Questions

I've got a function that is supposed to recursively loop through a control and
I've got the following code that I can't seem to get working: function drawTable($result)
I have the following code, which loops through an array of menuoptions and on
I have got the following very simple code: function init() { var articleTabs =
I have got the following issue, my function appends code to a string $string
I've got the following piece of Jquery: $(#collapse-menu > li > a).click(function() { $(this).toggleClass(expanded).toggleClass(collapsed).find(+
I've got the following jsp page and I'm using struts2-jquery-plugin-3.1.1 <script type=text/javascript> function openDialog()
I got a segmentation fault when invoked a function immediately following a pointer assignment.
I have been trying to call a C function that has the following signature
So I got the following scenario: $('.menu').find('a').each( function(el, val) { if( window.location.hash !== val.hash

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.