List of items beeing member of 2 or more groups

I’m just wondering, if there’s an easy way in JS to get a list of items which are member of 2 or more groups. I’m using groups to “tag” items eg. for location, billing, power measure point.
Then I would like to select all items being member of a certain location and billed to a certain account.

You can get the amount of groups, an item belongs to by:

items.getItem("TestItem").groupNames.length

I need to look up how to loop through all items (and excluding group items). Will do that when I am back home or another user might jump in.

Or try to find a post of rlkoshak where he helped me to get a list of all unlinked items. With the line above you can adopt it by yourself.

Hi Oliver, thanks for your input. That way I’d have to iterate through all items for finding the matching items. I’m just wondering if there’s no way to start on the group side, finding the overlapping members of multiple groups.

I have now a routine to match membership in two group:

function getGrpItems(grp1,grp2) {
    var x= items.getItem(grp1,true).members   
    var arr=[]
    x.forEach(i => {
        if (i.groupNames.toString().includes(grp2)) {
            arr.push(i)
        }
    })
    return arr;
}

In jruby, this is just a simple intersection of two arrays, using the & operator, which is a built-in feature of the language.

items_that_belong_to_both_groups = 
  Group1.members & Group2.members

Correct.
Or something like this:

var ItemsWithTwoOrMoreGroups = items.getItems().filter(f => f.groupNames.length >= 2);
console.log(ItemsWithTwoOrMoreGroups.toString());