Javascript 创建二维数组并在jquery中循环遍历它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26140640/
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
create two dimensional array and loop through it in jquery
提问by Shaggy
Objectives:
目标:
- Create Two dimension array in javascript/jquery
- Push data into it
- Loop through each key,value pair
- Call function in loop
- 在 javascript/jquery 中创建二维数组
- 将数据推入其中
- 循环遍历每个键值对
- 循环调用函数
Code:
代码:
var IDs = [];
/* Find Input elements and push its ID & Value into an array */
$('#divDynamicFields').find("input").each(function () {
IDs.push(this.id, $(this).val());
});
console.log(IDs); /* Now it prints string seprated by ',' */
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
$.each(key, function(innerKey, innerValue){
CallFunction(id,val);
/* Will This Work ? */
}
}
回答by Regent
The whole idea is to push to array not two elements, but an array, which consists of two elements:
整个想法是推送到数组不是两个元素,而是一个由两个元素组成的数组:
var IDs = [];
$('#divDynamicFields input').each(function()
{
IDs.push([this.id, $(this).val()]);
});
for (var i = 0; i < IDs.length; i++)
{
CallFunction(IDs[i][0], IDs[i][1]);
}
function CallFunction(id, value)
{
console.log("ID: " + id + ", value: " + value);
}
回答by Kesty
You have a couple of problems..
你有几个问题..
The first one is that you have to add the input as an array
第一个是您必须将输入添加为数组
IDs.push([this.id, $(this).val()]);
The second one is that you want to call the ids you just added together, you don't want to do a double loop.
第二个是你想调用你刚刚加在一起的id,你不想做双循环。
$.each(IDs, function(key, value) {
CallFunction(value[0],value[1]);
});
This is an example:
这是一个例子:
var IDs = [];
/* Find Input elements and push its ID & Value into an array */
$('#divDynamicFields').find("input").each(function () {
IDs.push([this.id, $(this).val()]);
});
console.log(IDs); /* Now it prints string seprated by ',' */
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
CallFunction(value[0],value[1]);
});
function CallFunction(id,val) {
console.log(id+","+val);
}
回答by Pratik
Use object for insertion
使用对象插入
var IDs = {};
$('#divDynamicFields').find("input").each(function () {
IDs[this.id]= $(this).val();
});
And similarly loop
和类似的循环
$.each(IDs , function (index, value) {
alert( index + ' : ' + value );
});
回答by Faris Zacina
Your push syntax and iteration is wrong. You should do something like:
您的推送语法和迭代是错误的。你应该做这样的事情:
var IDs = [];
IDs.push([1, 2]);
IDs.push([2, 3]);
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by Nikhil waghela
create two dimensional array
创建二维数组
MultiArray = new Array(5)
MultiArray [0] = new Array(2)
MultiArray [0][0] = "Tom"
MultiArray [0][1] = "scientist"
MultiArray [1] = new Array(2)
MultiArray [1][0] = "Beryl"
MultiArray [1][1] = "engineer"
https://trans4mind.com/personal_development/JavaScript/Array2D.htm
https://trans4mind.com/personal_development/JavaScript/Array2D.htm
回答by Dushyant Dagar
You can try this to create a dimensional array in jquery by using
您可以尝试使用此方法在 jquery 中创建一个多维数组
var IDs = [];
$('#divDynamicFields').find("input").each(function () {
IDs.push({
id: $(this).val()
});
});
console.log(IDs);
This will allow you to send your all data whatever you want to pass in array by ajax.
这将允许您通过 ajax 发送您想要在数组中传递的所有数据。
回答by test
var IDs = [];
IDs.push([1, 2]);
IDs.push([2, 3]);
/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
alert(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

