[英]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.