5
\$\begingroup\$

I want to output only the roles that contain the same group as the user.

// User can be part of many groups
const user = {
  groups: ["group2"]
};

// Roles can have many groups
// What you see here is the output or 2 different data source
// Thats why we have group duplication inside different role
const roles = [{
  name: "role1",
  groups: [{
    id: "group1"
  }]
}, {
  name: "role2",
  groups: [{
    id: "group1"
  }, {
    id: "group2"
  }]
}];

const result = roles.filter(role => role.groups.filter(group => user.groups.indexOf(group.id) > -1).length)
console.log(result);

Is there a better way by using reduce or something else?

\$\endgroup\$
  • 1
    \$\begingroup\$ Perhaps roles.filter(role => role.groups.find(group => user.groups.includes(group.id))) \$\endgroup\$ – Xotic750 Sep 20 '16 at 18:15
  • 1
    \$\begingroup\$ Also you have defined const roles and then you try to redefine it. \$\endgroup\$ – Xotic750 Sep 20 '16 at 18:35
5
\$\begingroup\$

This is little neater IMO.

// User can be part of many groups
const user = {
  groups: ["group2"]
};

// Roles can have many groups
// What you see here is the output or 2 different data source
// Thats why we have group duplication inside different role
const roles = [{
  name: "role1",
  groups: [{
    id: "group1"
  }]
}, {
  name: "role2",
  groups: [{
    id: "group1"
  }, {
    id: "group2"
  }]
}];

const result = roles.filter(role => role.groups.find(group => user.groups.includes(group.id)));
console.log(result);

\$\endgroup\$
2
\$\begingroup\$

filter method runs a given function on every item in the array and returns an array of all items for which the function returns true. This will help us to get each sub-array of the mainArray.

let mainArray = [
  ['a', 'b'],
  ['c', 'd'],
  ['e', 'f']
];

// Extract sub-arrays with filter
let temp = mainArray.filter(function(item, index, array){
        return array;
});
console.log({temp});

Now, we need to compare every sub-array with the subArray. For this we will use the every method. This method always return a boolean value true or false. That is base on the given function on every item in the array and returns true. If that function returns true for every true; what's that mean? Well, this mean every will return false, unless every item return true.

let searchArray = ['c', 'd'];

// Extract every letter from the sub-array 
temp = searchArray.every(function(item, index, array){
  console.log({item});
  return true;
});
console.log({temp});

Finally, includes method determines whether an array includes a certain value among its entries, returning true or false as appropriate.`

let searchArray = ['c', 'd'];


// Compare a letter in the searchArray.
let tempItem = 'c';
temp = searchArray.includes(tempItem);
console.log({temp});

Now, we can combine all and replace functions with the Arrow Function Syntax. And happy coding.

let mainArray = [
  ['a', 'b'],
  ['c', 'd'],
  ['e', 'f']
];
let searchArray = ['c', 'd'];

// All together
let results = mainArray.filter(array => array.every(item => searchArray.includes(item)));
console.log({results});

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.