代码之家  ›  专栏  ›  技术社区  ›  Anyname Donotcare

如何将属性添加到由对象数组组成的JSON对象的根中?

  •  0
  • Anyname Donotcare  · 技术社区  · 6 年前

    如何将属性添加到由对象数组组成的JSON对象的根中?

    如果我的JSON对象是这样的:

     [
       {
          "Id":"f2ac41c5-b214-48f6-ad40-9fc35c1aaad9",
          "Name":"W",
          "NumberOfWorkHours":8,
          "NumberOfShortDays":1,
          "WorkTimeRegulationId":"f5833075-2847-4cc3-834d-6138dd0dcd99"
       },
       {
          "Id":"5c267601-fcf2-4735-9e49-b4def3981648",
          "Name":"S",
          "NumberOfWorkHours":6,
          "NumberOfShortDays":0,
          "WorkTimeRegulationId":"8d14580e-278f-41d1-9239-8874be792580"
       }
    ]
    

    worktimeJSON.Id = $('.Js-WorkTime-id').val();
    worktimeJSON.Name = $('.Js-WorkTime-name').val();
    worktimeJSON.NumberOfAvailableRotations = $('.Js-WorkTime-rotations').val();
    

    并确保jQuery从输入中获取数据,但这不起作用。

    3 回复  |  直到 6 年前
        1
  •  3
  •   Nishant Dixit    6 年前

    这将更改数组中所有对象的属性,如果您特别想更改,请将索引用于exp->

    worktimeJSON[0].Id = $('.Js-WorkTime-id').val();
    worktimeJSON[0].Name = $('.Js-WorkTime-name').val();
    worktimeJSON[0].NumberOfAvailableRotations = $('.Js-WorkTime-rotations').val();
    

    var worktimeJSON = [
       {
          "Id":"f2ac41c5-b214-48f6-ad40-9fc35c1aaad9",
          "Name":"W",
          "NumberOfWorkHours":8,
          "NumberOfShortDays":1,
          "WorkTimeRegulationId":"f5833075-2847-4cc3-834d-6138dd0dcd99"
       },
       {
          "Id":"5c267601-fcf2-4735-9e49-b4def3981648",
          "Name":"S",
          "NumberOfWorkHours":6,
          "NumberOfShortDays":0,
          "WorkTimeRegulationId":"8d14580e-278f-41d1-9239-8874be792580"
       }
    ];
    
    worktimeJSON = worktimeJSON.map(function(val){
        val.Id = $('.Js-WorkTime-id').val();
        val.Name = $('.Js-WorkTime-name').val();
        val.NumberOfAvailableRotations = $('.Js-WorkTime-rotations').val();
        return val;
    });
        2
  •  2
  •   Cid Gordon    6 年前

    推就行了。

    let worktimeJSON = [
       {
          "Id":"f2ac41c5-b214-48f6-ad40-9fc35c1aaad9",
          "Name":"W",
          "NumberOfWorkHours":8,
          "NumberOfShortDays":1,
          "WorkTimeRegulationId":"f5833075-2847-4cc3-834d-6138dd0dcd99"
       },
       {
          "Id":"5c267601-fcf2-4735-9e49-b4def3981648",
          "Name":"S",
          "NumberOfWorkHours":6,
          "NumberOfShortDays":0,
          "WorkTimeRegulationId":"8d14580e-278f-41d1-9239-8874be792580"
       }
    ];
    
    worktimeJSON.push
    ({
    	id: "someID",
    	name: "toto",
    	WorkTimeRegulationId: 42
    });
    
    console.log(worktimeJSON);
        3
  •  0
  •   Anyname Donotcare    6 年前

    我把我的物体组织成这样:


     let WorkTimeRegulationViewModelJSON = {
                    Id: $('.Js-WorkTimeRegulation-id').val(),
                    Name: $('.Js-WorkTimeRegulation-name').val(),
                    NumberOfAvailableRotations: $('.Js-WorkTimeRegulation-rotations').val(),
                    AssignedWorkTimes: JSON.parse(worktimeJSON)
    
                };