向对象添加数据

时间:2017-07-06 05:40:44

标签: javascript angular

targetList: any = [];
public datavalue = [
  {
    value: ""
  }
]

this.httpService.service("Fields").subscribe(
  (
    targetList => {
      this.targetList = targetList;
      console.log(this.targetList);
      for (var i = 0; i < this.targetList.length; i++) {
        if (this.targetList[i].results.multiple == true) {
         this.targetList[i].results.data = this.datavalue;
        }
      }
    }
  ),
  error => { alert(`Can't get Fields.`); }
);

addvalue(i): void {
   var dataObj = {
    value: ""
   };
   this.targetList[i].results.data.push(dataObj);
 }

 removevalue(i, index) {
   this.targetList[i].results.data.splice(index, 1);
  }

我在我的服务中获得的数据如下

[  
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":true
     }
  },
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":true
     }
  },
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":false
     }
  }
]

在此结果中,如果multiple = true,那么如何添加此数据。

[  
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":true,
       "data" :[
                  {
                     value : ""
                  }
                ]
     }
  },
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":true
       "data" :[
                  {
                     value : ""
                  }
                ]
     }
  },
  {  
     "results":{  
       "field1":false,
       "field2":0,
       "multiple":false
     }
  }
]

帮助添加此格式,因为字段是动态的。我将其呈现在Html页面中。

                <div *ngIf="targetList.length>0">
                <!-- Index i for List of Elements-->
                <div *ngFor="let target of targetList; let i = index"> 
                  <fieldset>
                    <section>
                      <label class="label">{{target.results.field1}}</label>
                      <div *ngIf="target.results.multiple else singletext" class="table-responsive">
                        <table class="table table-bordered">
                          <tbody>
                            <!-- Index j for Multiple Data within an Element-->
                            <tr *ngFor="let data of target.results.data; let j = index">
                              <td>
                                <label class="input">
                                    <input type="text" name={{target.results.field1+i+j}} ngModel class="input-sm">
                                </label>
                              </td>
                              <td>
                                <label class="input">
                                    <a class="btn btn-danger btn-sm" (click)="removevalue(i,j)"><i class=" fa fa-remove"></i></a></label>
                              </td>
                            </tr>
                            <tr>
                              <td colspan="2"><a class="btn btn-success btn-sm" (click)="addvalue(i)">Add Value</a></td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <ng-template #singletext>
                        <label class="input">
                            <input type="text" name={{target.results.feild1+i}} ngModel  class="input-sm">
                        </label>
                      </ng-template>
                      <label class="label">{{ target.results.desc }}</label>
                    </section>
                  </fieldset>
                </div>
              </div>

在这一个中,如果我使用上面的代码,它也会向第二个对象添加datalist,而我只需要为第一个对象添加它。

2 个答案:

答案 0 :(得分:0)

循环遍历整个json对象,并在数组中包含的每个对象中查找'multiple'属性

 targetList: any = [];

    this.httpService.service("Fields").subscribe(
      (
        targetList => {
          this.targetList = targetList.json();
          console.log(this.targetList);

          for(let i=0 ; i<this.targetList.length ; i++ ){
              if(this.targetList[i].results.multiple){
                 // assuming you have data ready in 'dataValue' object
                 this.targetList[i].results['data'] = Object.assign({}, dataValue); // clone the object before setting it
              }
          }
        }
      ),
      error => { alert(`Can't get Fields.`); }
    );

答案 1 :(得分:0)

试试这个

var json = '[ { "results":{ "field1":false, "field2":0, "multiple":true } }, { "results":{ "field1":false, "field2":0, "multiple":true } }, { "results":{ "field1":false, "field2":0, "multiple":false } } ]'; json = JSON.parse(json); for(var i = 0; i < json.length; i++) { var obj = json[i]; if(obj.results.multiple){ json[i].results['data'] = []; } } 是回复

i