创建二维数组并在jquery中循环遍历它

时间:2014-10-01 11:45:19

标签: javascript jquery arrays

目的:

  1. 在javascript / jquery中创建二维数组
  2. 将数据推送到其中
  3. 遍历每个键,值对
  4. 循环调用函数
  5. 代码:

        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 ? */
          }
       }
    

7 个答案:

答案 0 :(得分:9)

整个想法是推送数组而不是两个元素,而是一个数组,它由两个元素组成:

JSFiddle

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);
}

答案 1 :(得分:1)

使用对象 插入

var IDs = {};
$('#divDynamicFields').find("input").each(function () {     
   IDs[this.id]= $(this).val(); 
}); 

同样循环

$.each(IDs , function (index, value) {
    alert( index + ' : ' + value );
});

答案 2 :(得分:1)

你有几个问题..

第一个是你必须将输入添加为数组

       IDs.push([this.id, $(this).val()]); 

第二个是你要调用你刚刚加在一起的id,你不想做双循环。

$.each(IDs, function(key, value) { 
      CallFunction(value[0],value[1]);
});

这是一个例子:

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);
}

JSFiddle

答案 3 :(得分:0)

您的推送语法和迭代错误。你应该做点什么:

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>

答案 4 :(得分:0)

创建二维数组

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

答案 5 :(得分:0)

您可以尝试使用

在jquery中创建一个维数组
var IDs = [];
 $('#divDynamicFields').find("input").each(function () { 
     IDs.push({
        id: $(this).val()           
     });
});

console.log(IDs);

这将允许您通过ajax发送您想要通过数组传递的所有数据。

答案 6 :(得分:-4)

&#13;
&#13;
var IDs = [];

IDs.push([1, 2]);
IDs.push([2, 3]);

/* Loop Through Each element in 2D array */
$.each(IDs, function(key, value) {
  alert(value);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;