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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:26:38+00:00 2026-05-16T03:26:38+00:00

I am using jQuery and JSON to pull data from a database. Each row

  • 0

I am using jQuery and JSON to pull data from a database. Each row of the database is a different product and each product has a number of properties associated with it.

What I’m trying to do in js, is create a named array for each product containing all of the products properties. I know how to pull the data from JSON. I know how to construct the Array.

What I don’t know is how to create an array name dynamically. Can someone help me out?

I am trying to name the array based on a field in the database. In the structure of my existing and working script, it’s referenced as data.cssid. I’d like to use the value of data.cssid as the name of the array and then populate the array.

$.getJSON("products.php",function(data){
    $.each(data.products, function(i,data){
        var data.cssid = new Array();
        data.cssid[0] = data.productid;
        ...
        etc
    });
});

I know the code above is completely wrong, but it illustrates the idea. Where I declare “var data.cssid”, I want to use the actual value of data.cssid as the name of the new array.

EDIT:

I’ve tried the methods mentioned here (except for eval). The code is below and is not really that different than my original post, except that I’m using a Object constructor.

$(document).ready(function(){
$.getJSON("productscript.php",function(data){
    $.each(data.products, function(i,data){
        var arrayName = data.cssid;
        obj[arrayName] = new Array();
        obj[arrayName][0] = data.productid;
        obj[arrayName][1] = data.productname;
        obj[arrayName][2] = data.cssid;
        obj[arrayName][3] = data.benefits;
        alert(obj[arrayName]); //WORKS
        alert(obj.shoe); //WORKS WHEN arrayName = shoe, otherwise undefined
    });
});
});

The alert for the non-specific obj[arrayName] works and shows the arrays in all their magnificence.
But, when I try to access a specific array by name alert(obj.shoe), it works only when the arrayName = shoe. Next iteration it fails and it can’t be accessed outside of this function.

I hope this helps clarify the problem and how to solve it. I really appreciate all of the input and am trying everything you guys suggest.

PROGRESS (THE SOLUTION):

$(document).ready(function(){
$.getJSON("productscript.php",function(data){
    $.each(data.products, function(i,data){
        var arrayName = data.cssid;
        window[arrayName] = new Array();
        var arr = window[data.cssid];
        arr[0] = data.productid;
        arr[1] = data.productname;
        arr[2] = data.cssid;
        arr[3] = data.benefits;
        alert(window[arrayName]); //WORKS
        alert(arrayName); //WORKS
        alert(shoe); //WORKS

    });
});
});
function showAlert() {
    alert(shoe); //WORKS when activated by button click
}

Thanks to everybody for your input.

  • 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-16T03:26:38+00:00Added an answer on May 16, 2026 at 3:26 am

    do you still want the array to be on the data object?

    data[data.cssid] = new Array();
    

    otherwise, you can assign it to any other object. assigning it to the window object, will make it globally available

    window[data.cssid] = new Array();
    

    … then if the value of data.cssid is "abc" then you can access it like so

    abc[0] = data.productid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check if application can open tel url: if ([[UIApplication sharedApplication]… May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer You could use Prototype.Version. May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer Naturally, it's your choice. Whatever logic you write, it will… May 16, 2026 at 9:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Heh, I'm using jQuery AJAX Call to pull data from a self hosted webservice
I'm using jQuery to grab some JSON data. I've stored it in a variable
I am trying to call an ASMX from jQuery using JSON and get a
I used to be pretty comfortable with using jquery for json, but I'm having
As the title may suggest, I have been using jQuery AJAX to try and
I'm using Ajax file upload function with its javascript / jQuery library . When
I am using jQuery Form Plugin http://jquery.malsup.com/form/ to submit my form. Here is the
I have a form that uses a JQuery Json post. I have 10+ textboxes
I'm unable to successfully post using jquery's ajax functionality. URL of the running page
I have an asp.net mvc calendar application (using jquery ui datepicker) and i am

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.