Forum Discussion

userg's avatar
userg
New Contributor II
2 years ago

Return true if values from my input 1 (list 1,list2,list3...so on) are available in input 2 list A

Hi Team

I have one list of values from input 1 and i need to compare all the values in my input 2 lists and insert into DB or send an error message "One or more values are not part of the input 1 List"

Example:

Input 1 - [a,b,c,d,e]

Input 2 has multiple lists:

list 1 -[c,a,e] - Insert into a table (Note: order and number values in the lists can be varying)

list 2 - [a,z] - do not insert

list 3 - [a,'',b] - do not insert

How can i achieve this,Thank you.

3 Replies

  • Hi userg ,

    Can you share how the input document looks like?

    Not sure if both inputs are accessible within a single object. Assuming they are, you can use the following expression to compare the values in the arrays:

     

    $Input2.map(val => { "values": val, "insert": val.map(v => $Input1.indexOf(v) != -1).filter(val => val == false).length == 0 ? true : false })

     

     Sample input data:

     

    [
        {
            "Input1": ["a", "b", "c", "d", "e", { "test": 1 }],
            "Input2": [
                ["c", "a", "e"],
                ["a", "z"],
                ["a", "", "b"],
                ["a", { "test": 2 }]
            ]
        }
    ]

     

    Output:

     

    [
      {
        "Input1":[ "a", "b", "c", "d", "e", { "test":1 } ],
        "Input2":[
          {
            "values":[ "c", "a", "e" ],
            "insert":true
          },
          {
            "values":[ "a", "z" ],
            "insert":false
          },
          {
            "values":[ "a", "", "b" ],
            "insert":false
          },
          {
            "values":[ "a", { "test":2 } ],
            "insert":false
          }
        ]
      }
    ]

     

    This expression adds an additional flag ( "insert" ), to the Input2 and if all the values in the array exist in the Input1, then insert will be true otherwise false. You can then split the Input2 array to get the object as individual input documents and pass through only the values where insert flag is equal to true. That can be done with a Filter snap.

    • userg's avatar
      userg
      New Contributor II

      Hi j_angelevski , I have mapper snap where I am getting data as below, after mapper snap I need route it to insert into a table in postgres db or send error message...

      Mapping table

      Expression - Target Path

      [a,b,c,d] - Original list

      [a,b]  - $Input list

      123 -  $External Id

      From above mapper i need to compare  input list to the original list and route it accordingly.Original list is a Hard-coded value in the mapper and I am getting lists and External IDs from a file.Thanks for your inputs

       

      From above i need campare

       

      • j_angelevski's avatar
        j_angelevski
        Contributor III

        In this case you can amend the original expression.

        $['Input list'].map(val => $['Original list'].indexOf(val)).filter(val => val == -1).length > 0 ? false : true

        This will return true only if all values within the Input list exist in the Original list.

        Output:

        You can route the data based on the insert field.