首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法设置未定义TypeScript的属性

无法设置未定义TypeScript的属性
EN

Stack Overflow用户
提问于 2017-12-19 22:46:06
回答 1查看 939关注 0票数 0

我想用数据设置对象,然后将其推送到另一个对象

代码语言:javascript
运行
复制
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'

我如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-19 22:54:58

您的代码中存在一些问题,因此我对其进行了一些清理

代码语言:javascript
运行
复制
// 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);

代码中的逻辑看起来有点扭曲,但如果不知道上下文就很难说出来

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47889422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档