如何使用JQuery创建多维数组-with .each()

时间:2017-05-10 11:55:01

标签: jquery each

我想用Jquery和.each函数创建以下数组:

我希望JSON看起来像这个:

{
            "calendar_id": "1",
            "dishs": [
                 {
                    "dish_id": "11111",
                    "amount": "234234"
                },
                 {
                    "dish_id": "11112",
                    "amount": "234234"
                },
                 {
                    "dish_id": "11113",
                    "amount": "234234"
                }, ...]
}

HTML

<input type="number" class="amount" id="amount_14793">
<input type="number" class="amount" id="amount_14794">
<input type="number" class="amount" id="amount_14795">

dish_id是id(即amount_14794) amount是该输入的val()

请注意calendar_id在数组的开头出现一次。

3 个答案:

答案 0 :(得分:2)

您需要一个空数组,然后使用每个

推送您构建的对象

&#13;
&#13;
var obj = [];
$('.amount').each(function(i,v){
    obj.push({'dish_id':$(v).attr('id'),'amount': $(v).val()});
});
console.log(obj);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" class="amount" id="amount_14793" value="1">
<input type="number" class="amount" id="amount_14794" value="2">
<input type="number" class="amount" id="amount_14795" value="3">
&#13;
&#13;
&#13; 如果你想要其他属性,你可以先用它们创建一个对象,然后将新对象追加到空的dishs属性中
&#13;
&#13;
var obj = {
  "calendar_id": "1",
  "dishs": []
};
$('.amount').each(function(i, v) {
  obj.dishs.push({
    'dish_id': $(v).attr('id'),
    'amount': $(v).val()
  });
});
console.log(obj);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" class="amount" id="amount_14793" value="1">
<input type="number" class="amount" id="amount_14794" value="2">
<input type="number" class="amount" id="amount_14795" value="3">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

可以通过以下方式完成:

&#13;
&#13;
$(document).ready(function() {
  var resultObj = {};
  resultObj.calendar_id = 1;
  resultObj.dishs = [];

  $(".amount").each(function() {

    var currentDish = {};
    currentDish.dish_id = $(this).attr("id").replace("amount_", ""); //set the dish_id by grabing only number out of element id

    currentDish.amount = $(this).val(); //set the amount

    resultObj.dishs.push(currentDish);

  });

  console.log(resultObj);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="number" value="12345" class="amount" id="amount_14793">
<input type="number" value="12345" class="amount" id="amount_14794">
<input type="number" value="12345" class="amount" id="amount_14795">
&#13;
&#13;
&#13;

答案 2 :(得分:0)

以下是您要查找的示例代码:

&#13;
&#13;
$("#submit").click(function(){
  var paramsToSend = {};
  var i = 1;
  var amount = ''
  $("input[name='number[]']").each(function(){
    paramsToSend['dish_id_' + i] = $(this).val();
    i++;
  });
  
   $("#dataToSend").html(JSON.stringify(paramsToSend));
   
   $.ajax({
         type: "POST",
         url: 'URL_HERE',
         data: {params:JSON.stringify(paramsToSend)},
         success: function(data) {
          console.log("SUCCESS!!!");
         }
   });
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inputsl">
<input name="number[]" type="number" class="amount" id="amount_14793">
<input name="number[]" type="number" class="amount" id="amount_14794">
<input name="number[]" type="number" class="amount" id="amount_14795">
</div>
<button id="submit">
Submit
</button>

<div id="dataToSend"></div>
&#13;
&#13;
&#13;