Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make an array with the ids from an associate array

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading