简体   繁体   English

如何在Javascript中动态命名数组?

[英]How can I dynamically name an array in Javascript?

I am using jQuery and JSON to pull data from a database. 我正在使用jQuery和JSON从数据库中提取数据。 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. 我要在js中执行的操作是为包含所有产品属性的每个产品创建一个命名数组。 I know how to pull the data from JSON. 我知道如何从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. 在我现有的工作脚本的结构中,它被称为data.cssid。 I'd like to use the value of data.cssid as the name of the array and then populate the array. 我想使用data.cssid的值作为数组的名称,然后填充数组。

$.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. 在声明“ var data.cssid”的地方,我想使用data.cssid的实际值作为新数组的名称。

EDIT: 编辑:

I've tried the methods mentioned here (except for eval). 我已经尝试过这里提到的方法(除eval之外)。 The code is below and is not really that different than my original post, except that I'm using a Object constructor. 下面的代码与我的原始文章没有太大不同,除了我使用的是Object构造函数。

$(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. 非特定obj [arrayName]的警报会起作用,并以其所有的华丽程度显示数组。 But, when I try to access a specific array by name alert(obj.shoe), it works only when the arrayName = shoe. 但是,当我尝试通过名称alert(obj.shoe)访问特定数组时,它仅在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. 感谢大家的投入。

do you still want the array to be on the data object? 您还希望数组位于data对象上吗?

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对象,将使其全局可用

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

... then if the value of data.cssid is "abc" then you can access it like so ...那么如果data.cssid值为"abc"则可以像这样访问它

abc[0] = data.productid;

No not possible. 没有不可能。 PHP has "variable variables"; PHP具有“变量变量”; Javascript does not. Javascript没有。

You could however assign the array to an extant object : 但是,您可以将数组分配给现有对象:

var obj = {};
var name = 'foo';
obj[name] = 1;

alert(obj.foo) // alerts "1"

Honestly not sure why you'd need that capability anyway. 坦率地说,不确定为什么您仍然需要该功能。 Can you post more code? 您可以发布更多代码吗? There's probably a decent workaround if the above doesn't help. 如果上述方法无济于事,可能有一个不错的解决方法。

You can set a property on an object by name like this: 您可以按如下名称在对象上设置属性:

var obj = {};  //Or, new Object()
obj[data.cssid] = new Array();

However, if you're just making a local variable, there is no point; 但是,如果您只是在做局部变量,那是没有意义的。 you can simply name the array newData (or something like that), as you will only ever be dealing with a single array at a time. 您可以简单地将数组命名为newData (或类似的名称),因为您一次只能处理一个数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript对象中动态引用变量键名? - How can I dynamically reference a variable key name in a Javascript object? 如何将数组名称存储在对象中以便以后动态选择? - How can I store an array name in an object for it to dynamically be chosen later? 如何在Javascript中动态获取JSON数组名称 - how to fetch JSON array name dynamically in Javascript Javascript:如何动态填充属性值数组? - Javascript: how can I populate an array of property values dynamically? 如何在 JavaScript 中动态创建数组/数组名 - How can I dynamically create an array/arraynames in JavaScript 当名称在Javascript中动态更改时,如何从字段名称中获取数据? - How can I get data from a field name when the name changes dynamically in Javascript? 如何在Javascript / AngularJS中动态设置foo等于Object Property Name? - How can I dynamically set foo equal to Object Property Name in Javascript/AngularJS? javascript是否可以动态构建要调用的函数名称? - javascript can I dynamically build a function name to call? 在Javascript对象数组中,如果该元素本身是一个对象数组,如何通过名称/ ID来定位特定的数组元素? - In a Javascript array of objects, how can I target a specific array element by name/ID if that element is an array of objects itself? Array 内的 Javascript Array - 如何调用子数组名称? - Javascript Array inside Array - how can I call the child array name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM