I am trying to make an array of IDs, those IDs would be collected from another associate array where they are working as index.
The associate array looks :
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'}
What I want to get :
[33,32,29]
What I am trying so far :
for (let index in this.singleData.permissions) {
this.singleData.permissions = this.singleData.permissions[index].id;
}
The error I am getting is :
ncaught TypeError: Cannot read properties of undefined (reading 'id')
But when I console it :
for (let index in this.singleData.permissions) {
console.log(this.singleData.permissions[index].id);
}
It gives the following result :
33
32
29
>Solution :
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);