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()
            });
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效