Note that you have an array and want map each element of the original array in something else. This will then generate an array of the same length as the original.
In this type of situation, the most ideal is to use the method Array.prototype.map
for map each element of the original array in something else. Read the documentation to understand how the map
works deep.
In your case, how do you want to map the object array to only one of the object properties (in this case, permission
), would look something like this:
const permissions = groupsmess.map((group) => group.permission);
Note that the map
is, in summary, a function applied to each element of the array. In this case, for each element of groupmess
, we are applying the function that returns the property permission
of the iteration element in question.
An example in browser:
const data = [
{ permission: 'group.default' },
{ permission: 'group.fundador' }
];
const permissions = data.map((object) => object.permission);
console.log(permissions);
Another alternative would be to create the array and map each element explicitly using a for
. Sort of like this:
const data = [
{ permission: 'group.default' },
{ permission: 'group.fundador' }
];
// Criamos um novo array:
const permissions = [];
// Percorremos sobre cada objeto do array original:
for (const object of data) {
// A cada iteração inserimos `object.permission` no novo array:
permissions.push(object.permission);
}
// Mesma saída:
console.log(permissions);
Have you tried, for example,
groupsmess[0].permission
?– Luiz Felipe
Yes but this only brings me the result of a specific array, example: groupsmes[1]. permission, but this comes from a database and this number of arrays generated can be infinite example:groupsmes[1]. permission, groupsmes[2]. permission, groupsmes[3]. permission.....
– Ellathet
And what exactly do you want to do?
– Luiz Felipe
I just want to read the data that my database behind, that Textrow is every column, and I don’t have a limit for that, so I can’t use the [array number]
– Ellathet
I understand that, but you can do endless things with the information you gave in the question. It is important that you detail a little better so that we can help you. Do you want, for example, to print each element? Want to use each element to create a list of values? In short, endless possibilities until we have a slightly more specific description. :)
– Luiz Felipe
These elements are things I have to buy, but I can’t just take the [0] === comparison, because I don’t know in which position the bank would bring me the array, if they were numbers I could use an organization, but they are strings.
– Ellathet
Summing up I would have to do in a general way, example if: If the information has A, B, C then I do F, G, D. But this has to be in a global way not specific.
– Ellathet
Have you tried using a
for
to iterate on the values? Or maybefor..of
? Because it’s an array, you need, in theory, to work on each value since you don’t know which to choose. There you can make a comparison (or any logic) in each iteration. I won’t be able to help more than that without more details. I don’t know what it meansA
,B
,C
,F
,G
orD
. You need to detail better.– Luiz Felipe
I didn’t try to use For. Summing up the array brings me this: [ Textrow { permission: 'group.default' }, Textrow { permission: 'group.founder' } ], I need this to become an array containing only 'group. <perm>'
– Ellathet