【问题标题】:Not able to copy the contents of one array to another in Angular 5无法在Angular 5中将一个数组的内容复制到另一个数组
【发布时间】:2018-05-25 11:19:17
【问题描述】:

我的组件中有一个像这样定义的空数组:

private dtoList=[];

我使用 HttpClient 调用服务从返回的后端获取数据和包含列表的对象:

    this.appService.getData()
        .subscribe((response) => {
          let value = response;
            //1st way
          this.dtoList = response['dtoList'];
          //2nd way
          this.dtoList = cloneDeep(response['dtoList']);
   });

在值字段中收到的响应是:

Object {success: false, description: null, dtoList: Array(2), status: null}

在响应对象中收到的列表没有被复制到目标列表中。 目标列表始终显示为未定义。

任何帮助将不胜感激。

【问题讨论】:

  • 你试过this.dtoList = response.dtoList; 吗?
  • 你的 cloneDeep() 是什么?
  • 你使用的是Http还是HttpClient
  • HttpClient @tricheriche
  • @YosvelQuintero 从不期望标签是正确的 ;)

标签: javascript angular angular5


【解决方案1】:

试试这个

response['dtoList'].forEach(element=>{ this.dtoList.push(element); });

【讨论】:

  • this.dtoList 在循环中始终未定义。
【解决方案2】:
your declaration is wrong for private dtoList=[]; 

declare like  dtoList:any=[]; than assign object like below
 constructor(){
    this.dtoList={success: false, description: null, dtoList: ['dat1','data2'], status: null}
    console.log(this.dtoList);
    console.log(this.dtoList.dtoList);
  }

【讨论】:

  • 它不起作用。我尝试分配的 dtoList 始终显示为未定义。
  • 在 JSON.stringify(value) 中转换您的响应
  • this.dtoList = JSON.parse(JSON.stringify(response['dtoList']));但无济于事。 this.dtoList 始终未定义。
  • this.dtoList = JSON.parse(JSON.stringify(response));比使用 console.log(this.dtoList.dtoList)
【解决方案3】:

尝试使用JSON.parse(JSON.stringify(obj)) 在数组中克隆您的对象

this.appService.getData()
        .subscribe((response) => {
          response['dtoList'].forEach((obj, index) => this.dtoList[index] = JSON.parse(JSON.stringify(obj)));
          console.log(this.dtoList)
});

【讨论】:

  • this.dtoList 显示为未定义。不知道为什么。
  • 您的response['dtoList'] 本身似乎是空的。你能证实吗?因为你在你的对象中显示dtoList: Array(2),它是空的。
【解决方案4】:

请以这种方式尝试。为结果列表项创建class(model),为getData方法设置通用Observable类型。

service.ts

@Injectable()
export class AppService {

  constructor(private http: HttpClient) { }

  getData(): Observable<ResultModel> {
    return this.http.get<ResultModel>('url for api');
  }

}

export class ResultModel {
  id: number;
  title: string;
}

component.ts

private dtoList: ResultModel[] = [];
 this.appService.getData()
    .subscribe((response) => {
        this.dtoList = response;
        // if here you want copy array, you can use spread operator
        // this.dtoList = [...response];
     });

【讨论】:

    【解决方案5】:

    发生的现象不是很清楚,但是如果我将返回的响应的内容复制到一个单独的方法中,它似乎可以工作。因此,我没有在匿名订阅函数中复制响应内容,而是将其传递给另一个方法进行复制。它是这样工作的。

    this.myService.getData().subscribe((response) => {
            let values = response['dtoList'];
            this.copyValues(values);
    });
    
    // the copyvalues method
    
    copyValues(list) {
        this.dtoList = list;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多