So I have a api call that brings back information about a user:
data: {
name: 'Some One',
age: 100,
gender: 'male'
// etc
}
And I'm setting this to a class object.
class User {
public data: any = {}
constructor(data: any):void {
this.updateData(data)
}
updateData(data) {
for (const property in data) {
if (Object.prototype.hasOwnProperty.call(data, property) &&
typeof data[property] !== 'function') {
this.data[property] = data[property]
}
}
}
}
So when you do a const user = new User(userInfoFromApi) and console.log(user), it sends up looking like the same way it came in from the API.
What I want to do is reference user.name instead of user.data.name. Is that possible?
Something I tried was setting
const self = this
And then self[property] = data[property] but Typescript did NOT like that.