我想用数据设置对象,然后将其推送到另一个对象
let globalSamples = {} as any;
let sample = { } as ISamplesDetail [];
sample = [];
for (let i = 0 ; i<this.prelevementLingette.samplesDetail.length; i++)
    {
      sample [i].id= this.old.samplesDetail[i].id;
      sample [i].reference=this.old.samplesDetail[i].reference;
}
globalSamples.push(sample);我得到了这个错误'Cannot set property 'reference' of undefined'
我如何解决这个问题?
发布于 2017-12-19 22:54:58
您的代码中存在一些问题,因此我对其进行了一些清理
// looks like this would be a proper type..
const globalSamples: ISamplesDetail[][] = [];
// can't assign object ({}) what should be an array, so..
// value doesn't change -> const
const sample: ISamplesDetail[] = [];
// it's strange that you iterate over 'this.prelevementLingette',
// but access 'this.old'
for (let i = 0 ; i < this.prelevementLingette.samplesDetail.length; i++) {
      sample[i] = {
          id: this.old.samplesDetail[i].id,
          reference: this.old.samplesDetail[i].reference
      }
}
// can't push to an object (should be array - [])
globalSamples.push(sample);代码中的逻辑看起来有点扭曲,但如果不知道上下文就很难说出来
https://stackoverflow.com/questions/47889422
复制相似问题