创建二维数组并在jquery中遍历它 [英] create two dimensional array and loop through it in jquery

查看:96
本文介绍了创建二维数组并在jquery中遍历它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

  1. 在javascript/jquery中创建二维数组
  2. 将数据放入其中
  3. 遍历每个键,值对
  4. 循环调用函数

代码:

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

推荐答案

整个想法是将数组推为两个元素,而不是两个元素:

The whole idea is to push to array not two elements, but an array, which consists of two elements:

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

这篇关于创建二维数组并在jquery中遍历它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆