I have the next line in Typescript
const addressFound: AddressPrimary = <AddressPrimary>(this.addressArray.find(addr => addr.id === id));
AddressPrimary
is a class that has a few variables in it one of them is id: number
another one is city: number
and there are more some of them are strings.
What exactly will the variable addressFound
hold when the line is done?
Will it hold a pointer to the class/data?
I’m asking because the method that holds this line "returns (addressFound)" and somewhere along the line I have a line myAddress: AddressPrimary = method(id)
and now when I do myAddress.id = 7;
the relevant class in array addressArray
changes so id gets the value 7.
2
Answers
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
So the addressFound will be first found element in addressArray with given id. Or undefined if non is found
Array.find()
returns the first element found in the array andundefined
if none found.If you want to get the index of the first occurance of the element then you should use
Array.findIndex()
instead; it will return-1
if none found.