Reputation: 99
I have this Array
numbers1: any=[
{ lat: 43.477254, lng: -3.780953 },
{ lat: 43.472739, lng: -3.781039 },
{ lat: 43.472498, lng: -3.780164 },
{ lat: 43.472023, lng: -3.780363 }
];
I want to fill that array dynamically, and I got it in this way:
list:any[]=[];
polygon(event){
this.list = [event.coords.lat, event.coords.lng];
this.number.push(this.list);}
But how can I set the values using the structure of Array number1?
{ lat: value, lng: value },
I've tried it but I have not got it. And Sorry by the way, im new on this.
Upvotes: 0
Views: 497
Reputation: 16705
I think you want to push
an object, instead of an array:
this.list = {
lat: event.coords.lat,
lng: event.coords.lng
};
this.number.push(this.list);
Upvotes: 3
Reputation: 52213
Your code references 3 different arrays: this.numbers1
, this.list
, and this.number
. It's not clear to me if that's intentional distinction, but it seems what you are trying to do is something like this:
// describe what the arrays contain
type Coord = { lat: number; lng: number; }
// define the array of those coords, initialize empty
numbers: Coord[] = [];
polygon(event){
// push the next coord (this will be type-checked)
this.numbers.push({ lat: event.coord.lat, lng: event.coord.lng });
}
Note that using a custom type Coord
instead of any
the compiler will guide you in what you are trying to do. For example this would be an error:
this.numbers.push([event.coord.lat, event.coord.lng]);
Compiles with error:
Error:
Argument of type 'any[]' is not assignable to parameter of type 'Coord'.
Property 'lat' is missing in type 'any[]'.
Upvotes: 0
Reputation: 59
You are trying to push an array rather then an object. And above you have an array named numbers1 and you are pushing in number. Use the correct one
Try this:
list:any={};
polygon(event)
{
this.list = {lat: event.coords.lat, lng: event.coords.lng};
this.number.push(this.list);
}
Upvotes: 0