更新现有的json对象

时间:2019-07-11 18:38:12

标签: javascript json

我的输入json为:

{   
   "setInfo":{  
      "userset1":{  
         "details1":[  
            "somedata1",
            "somedata2"
         ],
         "details2":[  
            "somedata3",
            "somedata4"
         ]
      },
      "userset2":{  
         "details3":[  
            "somedata5",
            "somedata6"
         ],
         "details4":[  
            "somedata7",
            "somedata8"
         ]
      }
   }
}

我想将其转换为以下输出json:

{  
   "setInfo":{  
      "userset1":{  
         "details1":{  
            "options":[  
               "op1",
               "op2",
               "op3"
            ]
         },
         "details2":{  
            "options":[  
               "op1",
               "op3"
            ]
         }
      },
      "userset2":{  
         "details3":{  
            "options":[  
               "op1"
            ]
         },
         "details4":{  
            "options":[  
               "op4"
            ]
         }
      }
   }
}

在上面的输入json somedata1中,somedata2 ... somedata8是我需要传递给服务端点的变量,这些变量将为我返回detail1的对应选项。..details4,即op1,op2等。 所以我想遍历上面的输入json并用我的服务返回的值更新上面的内容。

所以我尝试了以下代码。

//my inital service call to get data
myservice.getData().then(function(result){

// Below is console output from result.setInfo
//  setInfo:
//      userset1:
//        details1: (2) ["somedata1", "somedata2"]
//        details2: (2) ["somedata3", "somedata4"].
//      userset2:
//        details3: (2) ["somedata5", "somedata6"]
//        details4: (2) ["somedata7", "somedata8"]


//Now I tried looping through the json and update but not getting through as I feel something is incorrect below
_.each(result.setInfo, function (set, name) {

  $scope.sets[name] = {};  
    _.each(set, function (data1, name1) {

       //This is the service which gets corresponding options op1,op2 etc according to the params that were passed
       myservice.getArrayData({ data: data1}).function((res){

          //this is where I feel something is not correct. .pluck gets the correponding data fine but its the assignment which is not correct.
          $scope.sets[name].setInfo[name].options = _.pluck(res, 'name');

       });
    });
   });
});

使用上面的代码,我得到下面的json,这不是我期望的,

 {  
     "setInfo":{  
           "userset1":{  
              "options":[  
                 "op1",
                 "op2"
              ]
           },
           "userset2":{  
              "options":[  
                 "op1",
                 "op2"
              ]
           }
       }
    }

所以看看它如何删除上面的一些标签。

谁能指出我所缺少的内容。 抱歉,很长的帖子,但我想尽我所能。

-更新-

所以我更新为使用此代码

 $scope.setInfo[name][name1] = _.pluck(jobs, 'name');

有了这个,我有了进一步的进步,现在我的输出json变成了:

 {  
   "setInfo":{  
      "userset1":{  
         "details1":[  
            "op1",
            "op2",
            "op3"
         ],
         "details2":[  
            "op1",
            "op3"
         ]
      },
      "userset2":{  
         "details3":[  
            "op1"
         ],
         "details4":[  
            "op4"
         ]
      }
   }
}

因此,这里唯一缺少的是如何添加options标记。

我尝试过

 $scope.setInfo[name][name1].options = _.pluck(jobs, 'name');

但这给了我错误:

Cannot set property 'options' of undefined

0 个答案:

没有答案