dsf12123 2017-06-08 00:40
浏览 241
已采纳

jquery中的多维数组

My Jquery code

      var attrid        = [];
      var attrvalue     = [];

      $.each($(".attname1"), function(index){      

       if($('.getval'+index+'').val() != undefined){

          attrid.push($('.getval'+index+'').attr("id"));
          attrvalue.push($('.getval'+index+'').val());                  
        }

     });

    //outcome data
    ["5931", "5950", "5951", "5952"]  //id

    ["cas", "bsd", "Canvas", "Grey"] //name

The result format that I actually want:

$ary = array (
              [0] => array(
                      Id => 5931,
                      value=> cas,
                     ),
              [1] => array(
                      Id => 5950,
                      value=> bsd,
                     ),
              [2] => array(
                      Id => 5951,
                      value=> Canvas,
                     ),
              [3] => array(
                      Id => 5952,
                      value=> Grey,
                     )
       );

Question:

Above code are using Jquery to loop the data and store into array, I'm trying to create loop to create the result format the I want, but I failed to do that,anyone can provide me some sample code or idea which is can using Jquery to create the results format that I provided.Thank you.

  • 写回答

1条回答 默认 最新

  • dsifjgogw48491752 2017-06-08 00:44
    关注

    Make one array and push objects onto it:

    var ary = [];
    $('.attname1').each(function(index) {
        var getval = $('.getval' + index);
        if (getval.val()) {
            ary.push({
                id: getval.attr('id'),
                value: getval.val()
            });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行