Reputation: 440
I want to set object with data and then pushed it to another object
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);
I got this error 'Cannot set property 'reference' of undefined'
How can I resolve the problem ?
Upvotes: 0
Views: 1018
Reputation: 2721
There are a few problems in your code, so I've cleaned it up a bit
// 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);
It seems like the logic in your code is a bit twisted, but it's difficult to say without knowing the context
Upvotes: 1