简体   繁体   中英

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:

[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

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

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;
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM