dsf12123
2017-06-08 00:40
浏览 140

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.

图片转代码服务由CSDN问答提供 功能建议

我的Jquery代码

   var attrid = []; 
 var attrvalue = []; 
 
 $ .each($(“。attname1”),function(index){
 
 if($('。getval'+ index +')  ').val()!= undefined){
 
 attrid.push($('。getval'+ index +'')。attr(“id”)); 
 attrvalue.push($('。getval)  '+ index +'')。val()); 
} 
 
}); 
 
 //结果数据
 [“5931”,“5950”,“5951”,“5952”] /  / id 
 
 [“cas”,“bsd”,“Canvas”,“Gray”] // name 
   
 
 

结果格式 我实际上想要:

  $ ary = array(
 [0] => array(
 Id => 5931,
 value =>  ; cas,
),
 [1] =>数组(
 Id => 5950,
 value => bsd,
),
 [2] =>数组(
 Id  => 5951,
 值=> 画布,
),
 [3] => 数组(
 Id => 5952,
 value => Gray,
)
); 
   
 
 

问题:

上面的代码使用Jquery循环数据并存储到数组中,我正在尝试创建循环以创建我想要的结果格式,但我没有这样做,任何人 可以提供一些示例代码或想法,可以使用Jquery创建我提供的结果格式。谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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()
            });
        }
    });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题