1

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?

1 Answer 1

3

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)); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@yuracr9 glad to help, don't forget to upvote and park as answered :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.