3๐
I believe that your issue is with the associate array
. Usually you wouldnโt want to use the index and the id in the same way. This is because if you want to write the id 32 for example you would need an array with at least32 entries (you can not set the index higher then the length) => javascript dos not have have associative arrays
only 'normal' arrays
and objects
.
You can extract the ids with the .map
function like so.
const data = [{id: 1, name: 'users.update'}, {id: 2, name: 'users.update'}];
const ids = data.map(item => item.id);
console.log(ids);
1๐
In the first loop (the first item of this.singleData.permissions
), you change the variable this.singleData.permissions
itself and it is no longer an array.
For example, that is similar to:
let x = [1, 2, 3, 4, 5];
x = 1;
x = x[1]; // This throws
As you can see in the example, x
is no longer an array so it cannot access x[1]
.
I suggest changing the code to:
let temp = [];
for (let index in this.singleData.permissions) {
temp = [...temp, this.singleData.permissions[index].id];
}
this.singleData.permissions = temp;
1๐
const proxyIds = this.singleData.permissions.map((permission) => ({
permission.id;
}));
This should do the trick no ?
If you want to do it with a for in statement you need to assign the values to a new array. What you are currently doing is overriding the object you are iterating over in the first iteration of the statement. So in practice you start with
0: Proxy {id: 33, name: 'users.update'}
1: Proxy {id: 32, name: 'users.show'}
2: Proxy {id: 29, name: 'invoice-master.update'}
3: Proxy {id: 27, name: 'collection-deposite.index'}
And after the first iteration you assign a new value to the variable
this.singleData.permissions = this.singleData.permissions[index].id;
So this.singleData.permissions
is no longer an array but only the number 33( the id of the first element in the initial array ). So in the second iteration it throws an error because their isnโt a defined second element in the initial iterator.