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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:41:07+00:00 2026-05-31T12:41:07+00:00

I have a table array which looks like this: tablearray = [ {‘column1’: 1,

  • 0

I have a table array which looks like this:

tablearray =
[
    {'column1': 1, 'column2': 1, 'column3': 1, 'column4': 2},
    {'column1': 1, 'column2': 2, 'column3': 3, 'column4': 4},
    {'column1': 2, 'column2': 0, 'column3': 4, 'column4': 6}
]

I’m trying to make a function which takes the table array and an array of column names and makes a new object indexed by the column values. So

newObject = indexByColumnValues(tablearray, ['column1', 'column2']);

should result in an object like

newObject =
{
    1:
        {
            1: {'column1': 1, 'column2': 1, 'column3': 1, 'column4': 2},
            2: {'column1': 1, 'column2': 2, 'column3': 3, 'column4': 4}
        }
    2:
        {
            0: {'column1': 2, 'column2': 0, 'column3': 4, 'column4': 6}
        }
}

So

newObject[1][1]['column3'] = 1
newObject[1][2]['column4'] = 4
etc...

If the number of columns in the column name array ([‘column1’, ‘column2’] above) is known, the solution is not hard. But if I allow for any number of column names in this array, it becomes more difficult as there is indefinite recursion

newObject[tablearray[columnNameArray[0]][tablearray[columnNameArray[1]][tablearray[columnNameArray[2]]...

Here is one attempt. I tried to use a pointer to point to the dimensional depth of the newObject array. First, pointer = newObject. Then pointer = newObject[…[0]]. Then point = newObject[…[0]][…[1]]. And so on. This builds the object properly but then I do not have a way to assign a value to newObject[…[0]]…[…[k]].

function indexByColumnValues(object, columnNameArray)
{
    var newObject = {};

    for(i in object)
    {
        var index=[];

        for(j in columnNameArray)
        {
            index.push(object[i][columnNameArray[j]]);
        }

        var pointer = newObject;

        for(j in index)
        {
            if(pointer[index[j]] == undefined)
            {
                pointer[index[j]] = {};
            }

            pointer = pointer[index[j]];
        }

        //now pointer points to newObject[index[0]][index[1]]...[index[k]]
        //but I need to set newObject[...] above to be object[i].  How?
        //pointer = object[i]; //won't work
    }

    return newObject;
}

Any help or hints would be great here. 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-05-31T12:41:09+00:00Added an answer on May 31, 2026 at 12:41 pm

    You mention recursion, but you don’t use it in your code. This is a classic situation where recursion is the right tool. Here’s one implementation:

    function indexByColumnValues(table, cols) {
        // get the column we're indexing
        var col = cols[0],
            index = {},
            x, val;
        // find all values
        for (x=0; x<table.length; x++) {
            val = table[x][col];
            // add to index if necessary
            if (!index[val]) index[val] = [];
            // push this row
            index[val].push(table[x]);
        }
        // recurse if necessary
        if (cols.length > 1) {
            for (x in index) {
                if (index.hasOwnProperty(x)) {
                    // pass the filtered table and the next column
                    index[x] = indexByColumnValues(
                        index[x],
                        cols.slice(1)
                    );
                }                
            }
        }
        return index;
    }
    

    Note that, as @jfriend00 notes, you want the “leaf” of your index to be an array of matching rows, not a single object – it’s just coincidence that in your example you only have one matching row for your given data and set of columns. Usage:

    indexByColumnValues(tablearray, ['column1','column2']);​
    

    Output:

    {
        "1":{
            "1":[
                {"column1":1,"column2":1,"column3":1,"column4":2}
            ],
            "2":[
    
                {"column1":1,"column2":2,"column3":3,"column4":4}
            ]
        },
        "2":{
            "0":[
                {"column1":2,"column2":0,"column3":4,"column4":6}
            ]
        }
    }
    

    JsFiddle: http://jsfiddle.net/RRcRM/3/

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

Sidebar

Related Questions

I have a SQL table, which looks like this: id_question (int) | tags (varchar)
I have written a function to print database table to an array like this
I have a csv file which looks like this $lines[0] = text, with commas,
I have an in-memory table that might looks something like this: Favorite# Name Profession
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
While working with ActiveRecord I have a table which stores a serialized array of
I have a two-dimensional array (of Strings) which make up my data table (of
Say I have a class with a private dispatch table. $this->dispatch = array( 1
MY table looks like this id | string | foreign_id --------------------------- 1 | house
My SQLite table looks like this. --------------------------- |_id|str A |str B |str C |

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.