I need help with this code.
I working with "@angular/cli": "~12.0.5".
The createArray method receives an object and I want to transform the object to an array, but I have an error in 'userObj [key]'. I get the object (userObj) from Firebase through an http request and I can't change its structure.
This is the error message. -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'
Thanks!
const userObj = {
'SJKLDFAD903':{
id: '',
name: 'User 1'
},
'PLMKL-BAD89':{
id: '',
name: 'User 2'
},
'JHK34R-R903':{
id: '',
name: 'User 3'
}
}
export class UserModel{
id: string;
name: string;
}
private createArray(userObj){ /*(userObj: object)*/
const users: UserModel[] = [];
if (userObj == null) { return []; }
Object.keys(userObj).forEach(key => {
const user: UserModel = userObj[key];
user.id = key;
users.push(user);
});
return users;
}