6
\$\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\$
2
  • 1
    \$\begingroup\$ Perhaps roles.filter(role => role.groups.find(group => user.groups.includes(group.id))) \$\endgroup\$ Commented Sep 20, 2016 at 18:15
  • 1
    \$\begingroup\$ Also you have defined const roles and then you try to redefine it. \$\endgroup\$ Commented Sep 20, 2016 at 18:35

2 Answers 2

7
\$\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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.