I am trying to push an array of string into an array of object --> Accessright[];
selectedAccessRights: String[];
accessRights: Accessright[];
for (var i = 0; i < this.numOfPages; i++) {
this.selectedAccessRights.push(this.selectedPages[i].entityName);
//I have tried to the following as well, but it doesn't add everything to the accessRights object. Only the last element
//this.accessRights[i].entityName = this.selectedPages[i].entityName;
}
this.accessRights = this.accessRights.push(this.selectedAccessRights);
But the above line gives me an error
Argument of type '
String[]' is not assignable to parameter of type 'Accessright'. Type 'String[]' is missing the following properties from type 'Accessright':accessRightId,entityName,entityAttribute
Also tried add and insert from other post but didn't work for me.
Honestly out of ideas and debugged for so long. Would appreciate if anyone can take a look.
Update:
38: //Accessright response format
accessGroup: {accessGroupId: 1, accessGroupName: "AdminGroup", accessRights: Array(0), staffs: Array(0)}
accessRightId: 39
entityAttribute: ["charts"]
entityName: "EditDashboard"
__proto__: Object
39: Array(12) //My array
0:
accessRightId: 0
entityName: "Staff"
isDisabled: undefined
1:
accessRightId: 1
entityName: "AccessGroup"
isDisabled: undefined
__proto__: Object
Tried the any[] method but it's not working as my webservices is expecting an access right object.
3 Answers 3
selectedAccessRights: string;
accessRights: any = [];
for (var i = 0; i < this.numOfPages; i++) {
this.selectedAccessRights = this.selectedPages[i].entityName;
this.accessRights = this.accessRights.push(this.selectedAccessRights);
}
1 Comment
the push method of array returns the length of the array after the push is done, so firs of all its not returning a string[]. Also, when you push a value in array it added it to that array. So, change this.accessRights = this.accessRights.push(this.selectedAccessRights);
to
this.accessRights.push(this.selectedAccessRights);
Please refer the below link for detailed explanation of push method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
Comments
Updates:
I decided to change my backend to receive the array of string elements instead.
It was much easier than trying to insert an array of string into an object then send it to the backend.
Accessrighttype.selectedAccessRights: String[]=[]see the =[] else the array is null