Can someone assist me with this code issue?
I am currently using
"@angular/cli": "~12.0.5"
.
The issue lies within the createArray
method, where I need to convert an object into an array. However, I encounter an error specifically at 'userObj [key]'. The object (userObj) is retrieved from Firebase via an http request and its structure cannot be modified.
The error message reads as follows: -> 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 '{}'.
Thank you for your help!
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;
}