Issue
I have an array called rows of type TestEvent, and want to push to the array, i cant get to output the object i pushes it only shows undefined As you can see this.rows shows the arrays, but when i try to output a spesific array this.rows[0] i get undefined. (Typescript 2.7, Angular 5)
I've tried the following guides:
- http://robertdunawaypro.blogspot.no/2016/01/018-typescript-arrays-using-interface.html
- TypeScript push not available on interface array
- Typescript: push not available on custom typed array
import { TestEvent } from '../../models/event'
rows: TestEvent[] = []
public push(){
var test: TestEvent = {id: '222', category:'testcat', event_name: 'name'}
console.log(test) // Outputs the array
this.rows.push(test) //Push the array to this.rows
console.log(this.rows) //Outputs array of objects
consloe.log(this.rows[0]) //Outputs undefined
}
Event.ts
export interface TestEvent{
id?: string,
category?: string,
event_name?: string
}
Solution
That code has no problem and I tested on my local(typescript : 2.5 , angular5). And I got "row[0] : {id: "222", category: "testcat", event_name: "name"}"
interface TestEvent{
id?: string,
category?: string,
event_name?: string
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
rows: TestEvent[] = [];
ngOnInit(){
var test: TestEvent = {id: '222', category:'testcat', event_name: 'name'};
console.log(test); // Outputs the array
this.rows.push(test); //Push the array to this.rows
console.log(this.rows); //=> 0:{id: "222", category: "testcat", event_name: "name"}
console.log(this.rows[0]); // => {id: "222", category: "testcat",event_name: "name"}
}
}
Answered By - Jihoon Kwon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.