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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:31:47+00:00 2026-05-18T09:31:47+00:00

There is the following query results: (key1 and key2 could be any text) id

  • 0

There is the following query results: (key1 and key2 could be any text)

id   key1     key2     value

1    fred     apple    2
2    mary     orange   10
3    fred     banana   7
4    fred     orange   4
5    sarah    melon    5
...

and I wish to store the data in a grid (maybe as an array) looping all the records like this:

         apple    orange   banana  melon
fred        2        4         7     -
mary        -        10        -     -
sarah       -        -         -     5

In PHP this would be really easy, using associative arrays:

$result['fred']['apple'] = 2;

But in JavaScript associative arrays like this doesn’t work.
After reading tons of tutorial, all I could get was this:

arr=[];
arr[1]['apple'] = 2;

but arr['fred']['apple'] = 2; doesn’t work.
I tried arrays of objects, but objects properties can’t be free text.
The more I was reading tutorials, the more I got confused…

Any idea is welcome 🙂

  • 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-18T09:31:48+00:00Added an answer on May 18, 2026 at 9:31 am

    Just use a regular JavaScript object, which would ‘read’ the same way as your associative arrays. You have to remember to initialize them first as well.

    var obj = {};
    
    obj['fred'] = {};
    if('fred' in obj ){ } // can check for the presence of 'fred'
    if(obj.fred) { } // also checks for presence of 'fred'
    if(obj['fred']) { } // also checks for presence of 'fred'
    
    // The following statements would all work
    obj['fred']['apples'] = 1;
    obj.fred.apples = 1;
    obj['fred'].apples = 1;
    
    // or build or initialize the structure outright
    var obj = { fred: { apples: 1, oranges: 2 }, alice: { lemons: 1 } };
    

    If you’re looking over values, you might have something that looks like this:

    var people = ['fred', 'alice'];
    var fruit = ['apples', 'lemons'];
    
    var grid = {};
    for(var i = 0; i < people.length; i++){
        var name = people[i];
        if(name in grid == false){
            grid[name] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors
        }
    
        for(var j = 0; j < fruit.length; j++){
            var fruitName = fruit[j];
            grid[name][fruitName] = 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to store results from following query into another table, considering there is an
I have the following query. Is there an easy way that I could sort
Is there another way that I could do the following query SELECT @MinID =
I have following query, but it doesn't return any results for where clauses, even
Is there a reason why this query doesn't work? The following query will work
while executing the following query, i get an error that there's an error in
I am having trouble with the following MYSQL query and are hoping that there
is there a way to do the following in one Django query? MyModel.filter(attr=a).values('attr','fields','of','interest').annotate(count=Count('id')) MyModel.filter(attr=b).values('attr','fields','of','interest').annotate(count=Count('id'))
Is there a possibility to speedup a SQL-query like the following one? SELECT.... FROM
Im using the following query to target results that are exactly X days older

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.