Javascript 在javascript中声明空数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29254230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declare empty array in javascript
提问by user3944364
update
更新
My code that works. When page is loaded
我的代码有效。页面加载时
product= [[],[]];
then the code executed after ajax call:
然后在ajax调用后执行的代码:
$('#contextreload ul').each(function(i, ul) {
product.push([]);
});
$('#contextreload ul').each(function(i, ul) {
allline=i;
$('#reloadajax'+i+' li').each(function(lk, li) {
var lilk = $(li).html(); product[i][lk]=lilk;
// your code goes here
});
// your code goes here
});
To use eval(); in ajax response for this, with some changes in php file? /endupdate
使用 eval(); 在 ajax 响应中,在 php 文件中进行了一些更改? /结束更新
product[0]=[1,2,3,4];
产品[0]=[1,2,3,4];
product[1]=[a,b,x,z];
产品[1]=[a,b,x,z];
.
.
.
.
product[10]=[extra,extra,extra,extra];
产品[10]=[额外,额外,额外,额外];
When I load the page this is executed: product= [[],[],[],[],[],[],[],[],[],[]];
当我加载页面时,会执行以下操作: product= [[],[],[],[],[],[],[],[],[],[]];
But if I declare this, when I call ajax I can pushadd data only to this array (10 rows)
If I have 11 rows (product[10][0]and product[10][1]), the extra data will not be added.
After ajax call I need the extra data
: product= [[],[],[],[],[],[],[],[],[],[],**[11]**];
但是如果我声明这一点,当我调用 ajax 时,我只能将添加数据推送到这个数组(10 行)如果我有 11 行(product[10][0]和product[10][1]),则不会添加额外的数据。ajax 调用后,我需要额外的数据:product= [[],[],[],[],[],[],[],[],[],[],**[11]**];
This function is because I want to put data in array after loading ajax data from php file.
这个功能是因为我想在从php文件加载ajax数据后将数据放入数组中。
$('#contextreload ul').each(function(i, ul) {
<strike> var product = $(ul).html(); </strike>
allline = i;
$('#reloadajax'+i+' li').each(function(lk, li) {
var lilk = $(li).html();
product[i][lk]=lilk;
alert(lilk+lk);
// your code goes here
});
// your code goes here
});
}
采纳答案by R3tep
In the succes of your ajax call use the function push()
在您的 ajax 调用成功中使用该函数 push()
product.push([]);
This adds an array at the last index of product. Like that ,the index 10is created and you have an extra data.
这会在 的最后一个索引处添加一个数组product。像这样,索引10被创建,你有一个额外的数据。
If you want to add a dynamic number of rows, use this code :
如果要添加动态行数,请使用以下代码:
var number_of_new_row = 11; // 11 for example, you can use any number >= 0
for(; number_of_new_row--;)
product.push([]);
Another way
其它的办法
In your ajax return save the new length of your array productin a global variable.
And use it before your loop to reset your array and initialize it with the new length.
在您的 ajax 返回中,将数组的新长度保存product在全局变量中。并在循环之前使用它来重置数组并使用新长度对其进行初始化。
var lengthArray = 10; // update the value in the callback of your ajax call
And your loop :
而你的循环:
var product = [];
for(; lengthArray--;)
product.push([]);
$('#contextreload ul').each(function(i, ul) {
//<strike> var product = $(ul).html(); </strike>
allline = i;
$('#reloadajax'+i+' li').each(function(lk, li) {
var lilk = $(li).html();
product[i][lk]=lilk;
alert(lilk+lk);
// your code goes here
});
// your code goes here
});
回答by fedmich
Note: this line of your code produces a string, not an array.
注意:这行代码生成一个字符串,而不是一个数组。
var product = $(ul).html(); //returns string not an array
what you need is something like
你需要的是像
var product_arr = {}; // an object or
var product_arr = []; // an array
回答by Vijay
The following code used to declare empty array in javascript
以下代码用于在javascript中声明空数组
var product_arr = new Array(); //declaring empty array
console.log(product_arr);

