Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically name an array in Javascript?

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.

like image 427
Duffy Dolan Avatar asked Jul 20 '10 16:07

Duffy Dolan


People also ask

How do you give an array a dynamic name?

So it would be more like: var arrays = { ArrayTop: [], ArrayNorth: [] }; function setArray(a, b, c){ arrays['Array' + a][b] = c; } setArray('Top', 5, 100); I would not recommend using eval. Eval is not meant for this kind of dynamic evaluation and it is a huge performance hit.

How do you name an array in JavaScript?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

Can we declare array dynamically?

A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required.

How are JavaScript arrays dynamic?

JavaScript Arrays are Dynamic in nature it means, the length of array can be modified at run time (when necessary). Array length can be specified after declaring array as shown in the following example.


1 Answers

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;
like image 62
David Hedlund Avatar answered Sep 22 '22 17:09

David Hedlund