get unique index list by filtering other list

Hi dear all,

 i'm trying to get a index array from following list. The scenario is that one item refers to one uses' product. If one user has more products, then more items will be created. 

And now im trying to get a list shows all 'individual''s "first" index.Duplicated item should not be collected into the list.

The expecting output list would be:

[1,3,8]

as you can see all individual's first index should be collected into this output list. Currently im using wherecontains() but i cannot detect and remove the duplicated items.

My current output list:

[1,2,3,4,7,8]

Input List:

[[
id:1,
name:Tom,
userType:Individual,
product:Laptop

],
[
id:2,
name:Tom,
userType:Individual,
product:Phone
],
[
id:3,
name:Jerry,
userType:Individual,
product:Laptop
],
[
id:4,
name:Jerry,
userType:Individual,
product:Mac
],
[
id:5,
name:Spike,
userType:Entity,
product:T-bone
],
[
id:6,
name:Spike,
userType:Entity,
product:Laptop
],
[
id:7,
name:Tom,
userType:Individual,
product:Mac
]]

Thanks for your kind help and response.

  Discussion posts and replies are publicly visible

Parents
  • First to confirm, it appears the output should be 1,3,5 vs 1,3,8 correct?  As there are only 7 items in the list and Spike's first index is 5.  Assuming so, and assuming your list is sorted in ID as above, give this a try:

    a!localVariables(
      local!data: {
        {id:1,name:"Tom",userType:"Individual",product:"Laptop"},
        {id:2,name:"Tom",userType:"Individual",product:"Phone"},
        {id:3,name:"Jerry",userType:"Individual",product:"Laptop"},
        {id:4,name:"Jerry",userType:"Individual",product:"Mac"},
        {id:5,name:"Spike",userType:"Entity",product:"T-bone"},
        {id:6,name:"Spike",userType:"Entity",product:"Laptop"},
        {id:7,name:"Tom",userType:"Individual",product:"Mac"}
      },
      
      a!flatten(
        a!forEach(
          items: union(local!data.name,local!data.name),
          expression: {
            wherecontains(fv!item,apply(fn!tostring,local!data.name))[1]
          }
        )
      )
    )

  • Hi Chris, thanks for your response, im sorry that i forgot to update the list ,there should be a id8 after the id7 like this:

    local!data: {
    {id:1,name:"Tom",userType:"Individual",product:"Laptop"},
    {id:2,name:"Tom",userType:"Individual",product:"Phone"},
    {id:3,name:"Jerry",userType:"Individual",product:"Laptop"},
    {id:4,name:"Jerry",userType:"Individual",product:"Mac"},
    {id:5,name:"Spike",userType:"Entity",product:"T-bone"},
    {id:6,name:"Spike",userType:"Entity",product:"Laptop"},
    {id:7,name:"Tom",userType:"Individual",product:"Mac"},

    {id:8,name:"Mammy Two Shoes",userType:"Individual",product:"Mac"}
    },

    so suppose the output list should be [1,3,8] , the Spike should not be collected since its userType is "entity". I only want to collect the "individual" party, should i still use this union() ?

  • No problem - yes, union() will give you the distinct list of names to return from.  We can limit the results to Individuals only by removing the Entity rows, note the new local!cleaned variable:

    a!localVariables(
      local!data: {
        {id:1,name:"Tom",userType:"Individual",product:"Laptop"},
        {id:2,name:"Tom",userType:"Individual",product:"Phone"},
        {id:3,name:"Jerry",userType:"Individual",product:"Laptop"},
        {id:4,name:"Jerry",userType:"Individual",product:"Mac"},
        {id:5,name:"Spike",userType:"Entity",product:"T-bone"},
        {id:6,name:"Spike",userType:"Entity",product:"Laptop"},
        {id:7,name:"Tom",userType:"Individual",product:"Mac"},
        {id:8,name:"Mammy Two Shoes",userType:"Individual",product:"Mac"}
      },
      local!cleaned:  index(local!data,wherecontains("Individual",apply(fn!tostring,local!data.userType)),{}),
    
      a!flatten(
        a!forEach(
          items: union(local!cleaned.name,local!cleaned.name),
          expression: {
            wherecontains(fv!item,apply(fn!tostring,local!data.name))[1]
          }
        )
      )
    )

  • it works very well for me now, million thanks Chris!

Reply Children
No Data