How I remove the String from inside a Sequelize return

Asked

Viewed 65 times

0

I made a select by Sequelize and it brings me this:

[
    TextRow { permission: 'group.default' },
    TextRow { permission: 'group.fundador' }
]

But how do I get only the 'group.#' of that array? I want something like:

['group.default', 'group.fundador']

My code so far:

const groupsmess = await GroupsSelect(useruuiddb.uuid)
console.log(groupsmess[0])
  • 1

    Have you tried, for example, groupsmess[0].permission?

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

  • And what exactly do you want to do?

  • 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]

  • 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. :)

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

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

  • Have you tried using a for to iterate on the values? Or maybe for..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 means A, B, C, F, G or D. You need to detail better.

  • 1

    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>'

Show 4 more comments

1 answer

0


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.