0

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.

6
  • Please provide the definition of the Accessright type. Commented Oct 19, 2019 at 12:49
  • Can you Share your code on stackblitz Commented Oct 19, 2019 at 12:51
  • 1
    To me, it looks like you're trying to push a string array into a Array of Accessright that clearly doesn't support it. You need to either modify the definition of AccessRight, or make the variable type any, Commented Oct 19, 2019 at 12:51
  • @KiranMistry will try to do it soon Commented Oct 19, 2019 at 13:10
  • 1
    @Mason, you need initialize an array before push selectedAccessRights: String[]=[] see the =[] else the array is null Commented Oct 19, 2019 at 13:28

3 Answers 3

1
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);

     }
Sign up to request clarification or add additional context in comments.

1 Comment

please explain your code with solution it would be better
1

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.