Reputation: 49
I have a class:
export class Party {
constructor ( public title: string,
public lan1: number,
public lan2: number ) { }
}
I have a service:
import { Party } from '../classes/party';
export class PartyService {
parties: Party[] = [];
addData(title: string, lan1: number, lan2: number) {
this.parties.push(new Party(title, lan1, lan2));
}
getData(): Party[] {
return this.parties;
}
}
I have a component:
export class AppComponent implements OnInit {
parties: Party[] = [];
title = 'app';
party_location: Location[] = [
{ lat: 123, lng: 23 }
];
constructor(private partyService: PartyService){}
ngOnInit() {
this.parties = this.partyService.getData();
}
}
And I want to add parties properties to party_location like this:
for (let item in this.parties) { this.party_location.push(new Location(item.lan1, item.lan2)); }
But I can't because typescript doesn't see lan1 lan2 properties in item. How I can do this?
Upvotes: 1
Views: 4428
Reputation: 250326
You need to use for..of
to iterate the array. for..in
iterates the properties of an object, for..of
iterates an array elements:
for (let item of this.parties) {
this.party_location.push(new Location(item.lan1, item.lan2));
}
Upvotes: 3