Extract Array data in Javascript

1

I have array with the workgroups that the session user belongs to, for example if I write like this:

return $data.teste02[0].group_id.name

It returns me the first group that this user belongs to, which in this case would be administrator But there are cases that the user of the session will belong to more than one group, and I need to know all the workgroups it belongs to, so I thought I'd make a for to access all groups, thinking about that I made the following code:

for (var x = 0; x <= $data.teste02.length; x++){
   return $data.teste02[x].group_id.name;
}

But even so he only returns me the working group from the first position. If you have any idea how I can achieve this.

    
asked by anonymous 28.08.2017 / 15:38

3 answers

3

When giving a return within a for, it implicitly applies a break so its repetition structure described above will stop at the first interaction.

It is very common to use this if you are looking for a specific value, or it will give a binary return.

So, since it is possible to have multiple groups, the solution is to accumulate in another array and return only the group names.

var grupos = [];
for (var x in $data.teste02){
   grupos.push($data.teste02[x].group_id.name);
}
return grupos ;

With this you could validate if the user is in a group using the indexOf for example.

if(grupos.indexOf('administrador') != -1){
  //Grupo administrador
}
    
28.08.2017 / 15:42
3

If you want to create a new array with the names you can use .map() like this:

var grupos = $data.teste02.map(el => el.group_id.name);

So you get a new array, with the same size but only with group_id.name .

    
28.08.2017 / 15:48
3
  

This is a new technology, part of the ECMAScript 2015 (ES6) standard.

As a curiosity, using generators is also possible to do basically following its logic. Here's an example:

function* get_group_names(data)
{
    for (let group of data.teste2) {
        yield group.group_id.name;
    }
}

Or alternatively, as commented by Sergio, use {group_id} in for , this being equivalent to group.group_by of previous code:

function* get_group_names(data)
{
    for (let {group_id} of data.teste2) {
        yield group_id.name;
    }
}

So, to get the list of group names, just convert the generator to array :

const groups = Array.from(get_group_names($data));

Or even traverse it through a loop of repetition:

for (let group of get_group_names($data)) {
    console.log(group);
}

References:

function * - JavaScript | MDN

for ... of - JavaScript | MDN

How does yield * work in Javascript?

See working below.

const $data = {
  'teste2': [{
    'group_id': {
      'name': 'Grupo 1'
    },

  }, {
    'group_id': {
      'name': 'Grupo 2'
    },

  }, {
    'group_id': {
      'name': 'Grupo 3'
    },

  }]
};

function* get_group_names(data) {
  for (let group of data.teste2) {
    yield group.group_id.name;
  }
}

// Converte o gerador para array:
console.log(Array.from(get_group_names($data)));

// Ou percorre o gerador com um laço de repetição:
for (let group of get_group_names($data)) {
  console.log(group);
}
    
28.08.2017 / 15:54