Mule 4: DW2 - Array of objects, distinct by object fieldHow do I (or can I) SELECT DISTINCT on multiple columns?SQL to find the number of distinct values in a columnIs there any difference between GROUP BY and DISTINCTLINQ's Distinct() on a particular propertyHow can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL/mysql - Select distinct/UNIQUE but return all columns?SAS distinct in proc sql vs proc sort nodupkeyHow do I include 'undefined' in the array returned by mongodb's distinct() operator when the field does not exist?How to make array distinctDataweave XML-XML Transformation “Cannot coerce a :array to a :string.”

Why does the U.S. military maintain their own weather satellites?

How to save money by shopping at a variety of grocery stores?

Should a TA point out a professor's mistake while attending their lecture?

Using font to highlight a god's speech in dialogue

Does the Freedom of Movement spell prevent petrification by the Flesh to Stone spell?

What is the practical impact of using System.Random which is not cryptographically random?

Break down the phrase "shitsurei shinakereba naranaindesu"

LINQ Extension methods MinBy and MaxBy

How can I portray a character with no fear of death, without them sounding utterly bored?

A truncated alternating sum of product of binomial terms

I failed to respond to a potential advisor

Heuristic argument for the Riemann Hypothesis

What caused the end of cybernetic implants?

How to investigate an unknown 1.5GB file named "sudo" in my Linux home directory?

Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code

How does the search space affect the speed of an ILP solver?

How can I store milk for long periods of time?

Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?

Tikz: Draw simplified BLE-Stack

When you have to wait for a short time

I was given someone else's visa, stamped in my passport

Can the inductive kick be discharged without a freewheeling diode, in this example?

Quick Tilepaint Puzzles: Corridors and Corners

Does FERPA require parental notification of disability assessment?



Mule 4: DW2 - Array of objects, distinct by object field


How do I (or can I) SELECT DISTINCT on multiple columns?SQL to find the number of distinct values in a columnIs there any difference between GROUP BY and DISTINCTLINQ's Distinct() on a particular propertyHow can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL/mysql - Select distinct/UNIQUE but return all columns?SAS distinct in proc sql vs proc sort nodupkeyHow do I include 'undefined' in the array returned by mongodb's distinct() operator when the field does not exist?How to make array distinctDataweave XML-XML Transformation “Cannot coerce a :array to a :string.”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want an array of objects that are distinct by a certain field.
The array is already sorted by this field so identifying which fields to remove should follow this: the value of the previous field should be different from current value of the field.



For example, this array



[A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']


should transform to



[A:'1',B:'1',A:'2',B:'2']


I tried the following:



%dw 2.0
output application/json
var payload=[A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
---
(payload map (
(a,i) -> ( (a) if payload[i-1].A != $.A )
))


but it does not work. If I do not use the current item ($) then it works like this



 (a,i) -> ( (a) if payload[i-1].A != '2' ) 


But I need the current and previous items to be present to determine that current item is new (not equal for previous one).










share|improve this question
































    0















    I want an array of objects that are distinct by a certain field.
    The array is already sorted by this field so identifying which fields to remove should follow this: the value of the previous field should be different from current value of the field.



    For example, this array



    [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']


    should transform to



    [A:'1',B:'1',A:'2',B:'2']


    I tried the following:



    %dw 2.0
    output application/json
    var payload=[A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
    ---
    (payload map (
    (a,i) -> ( (a) if payload[i-1].A != $.A )
    ))


    but it does not work. If I do not use the current item ($) then it works like this



     (a,i) -> ( (a) if payload[i-1].A != '2' ) 


    But I need the current and previous items to be present to determine that current item is new (not equal for previous one).










    share|improve this question




























      0












      0








      0








      I want an array of objects that are distinct by a certain field.
      The array is already sorted by this field so identifying which fields to remove should follow this: the value of the previous field should be different from current value of the field.



      For example, this array



      [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']


      should transform to



      [A:'1',B:'1',A:'2',B:'2']


      I tried the following:



      %dw 2.0
      output application/json
      var payload=[A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
      ---
      (payload map (
      (a,i) -> ( (a) if payload[i-1].A != $.A )
      ))


      but it does not work. If I do not use the current item ($) then it works like this



       (a,i) -> ( (a) if payload[i-1].A != '2' ) 


      But I need the current and previous items to be present to determine that current item is new (not equal for previous one).










      share|improve this question
















      I want an array of objects that are distinct by a certain field.
      The array is already sorted by this field so identifying which fields to remove should follow this: the value of the previous field should be different from current value of the field.



      For example, this array



      [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']


      should transform to



      [A:'1',B:'1',A:'2',B:'2']


      I tried the following:



      %dw 2.0
      output application/json
      var payload=[A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
      ---
      (payload map (
      (a,i) -> ( (a) if payload[i-1].A != $.A )
      ))


      but it does not work. If I do not use the current item ($) then it works like this



       (a,i) -> ( (a) if payload[i-1].A != '2' ) 


      But I need the current and previous items to be present to determine that current item is new (not equal for previous one).







      distinct dataweave mule4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 3:08









      Kingsley

      4,3314 gold badges14 silver badges31 bronze badges




      4,3314 gold badges14 silver badges31 bronze badges










      asked Mar 27 at 22:46









      AlexAlex

      3,4852 gold badges14 silver badges50 bronze badges




      3,4852 gold badges14 silver badges50 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          3















          You should be able to ignore the fact that your array is sorted: you don't need the convenience of knowing the current value is distinct from the previous. You can use distinctBy instead:



          %dw 2.0
          output application/json

          var arr = [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
          ---
          arr distinctBy $.A


          Returns



          [

          "A": "1",
          "B": "1"
          ,

          "A": "2",
          "B": "2"

          ]


          Here are the docs for distinctBy if you're interested: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-distinctby






          share|improve this answer


































            1















            I will give a more general answer that will not only distinct by 'A' but also distinct by 'B' because it is not clear in the question wording if doing distinct by only 'A' is enough. Although your dataweave tries to distinct only by 'A', your example has only two equal As and no two equal Bs - which is one specific case.
            So I will assume that no two As should be equal and no two Bs should be equal.



            Some examples with desired outputs:



            Example(i): [A:'1',B:'1',A:'2',B:'2',A:'3',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
            Example(ii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
            Example(iii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3'] -> [A:'1',B:'1',A:'2',B:'2']
            Example(iv): ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2',"A":'2',"B":'2'] -> [A:'1',B:'1',A:'2',B:'2']


            1. One might be tempted to do arr distinctBy ($.A + $.B). But this would work only in the case of example(ii) because it will distinct by combined A and B. Example(i) and example(iii) will remain unchanged. Example(iv) will transform to ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2'] .

            2. So to achieve stated goal of no two As are equal and no two Bs are equal, we have to first distinct by 'A', then distinct by 'B' and then find the common overlapping array between the two. So I am using filter and contains to achieve this like below:

            %dw 2.0
            output application/json
            var arr = ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2']
            ---
            (arr distinctBy $.A) filter ((arr distinctBy $.B) contains $)





            share|improve this answer





























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387628%2fmule-4-dw2-array-of-objects-distinct-by-object-field%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3















              You should be able to ignore the fact that your array is sorted: you don't need the convenience of knowing the current value is distinct from the previous. You can use distinctBy instead:



              %dw 2.0
              output application/json

              var arr = [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
              ---
              arr distinctBy $.A


              Returns



              [

              "A": "1",
              "B": "1"
              ,

              "A": "2",
              "B": "2"

              ]


              Here are the docs for distinctBy if you're interested: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-distinctby






              share|improve this answer































                3















                You should be able to ignore the fact that your array is sorted: you don't need the convenience of knowing the current value is distinct from the previous. You can use distinctBy instead:



                %dw 2.0
                output application/json

                var arr = [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
                ---
                arr distinctBy $.A


                Returns



                [

                "A": "1",
                "B": "1"
                ,

                "A": "2",
                "B": "2"

                ]


                Here are the docs for distinctBy if you're interested: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-distinctby






                share|improve this answer





























                  3














                  3










                  3









                  You should be able to ignore the fact that your array is sorted: you don't need the convenience of knowing the current value is distinct from the previous. You can use distinctBy instead:



                  %dw 2.0
                  output application/json

                  var arr = [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
                  ---
                  arr distinctBy $.A


                  Returns



                  [

                  "A": "1",
                  "B": "1"
                  ,

                  "A": "2",
                  "B": "2"

                  ]


                  Here are the docs for distinctBy if you're interested: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-distinctby






                  share|improve this answer















                  You should be able to ignore the fact that your array is sorted: you don't need the convenience of knowing the current value is distinct from the previous. You can use distinctBy instead:



                  %dw 2.0
                  output application/json

                  var arr = [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3']
                  ---
                  arr distinctBy $.A


                  Returns



                  [

                  "A": "1",
                  "B": "1"
                  ,

                  "A": "2",
                  "B": "2"

                  ]


                  Here are the docs for distinctBy if you're interested: https://docs.mulesoft.com/mule-runtime/4.1/dw-core-functions-distinctby







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 27 at 23:25

























                  answered Mar 27 at 23:07









                  jerneyjerney

                  1,5221 gold badge8 silver badges24 bronze badges




                  1,5221 gold badge8 silver badges24 bronze badges


























                      1















                      I will give a more general answer that will not only distinct by 'A' but also distinct by 'B' because it is not clear in the question wording if doing distinct by only 'A' is enough. Although your dataweave tries to distinct only by 'A', your example has only two equal As and no two equal Bs - which is one specific case.
                      So I will assume that no two As should be equal and no two Bs should be equal.



                      Some examples with desired outputs:



                      Example(i): [A:'1',B:'1',A:'2',B:'2',A:'3',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                      Example(ii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                      Example(iii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3'] -> [A:'1',B:'1',A:'2',B:'2']
                      Example(iv): ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2',"A":'2',"B":'2'] -> [A:'1',B:'1',A:'2',B:'2']


                      1. One might be tempted to do arr distinctBy ($.A + $.B). But this would work only in the case of example(ii) because it will distinct by combined A and B. Example(i) and example(iii) will remain unchanged. Example(iv) will transform to ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2'] .

                      2. So to achieve stated goal of no two As are equal and no two Bs are equal, we have to first distinct by 'A', then distinct by 'B' and then find the common overlapping array between the two. So I am using filter and contains to achieve this like below:

                      %dw 2.0
                      output application/json
                      var arr = ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2']
                      ---
                      (arr distinctBy $.A) filter ((arr distinctBy $.B) contains $)





                      share|improve this answer































                        1















                        I will give a more general answer that will not only distinct by 'A' but also distinct by 'B' because it is not clear in the question wording if doing distinct by only 'A' is enough. Although your dataweave tries to distinct only by 'A', your example has only two equal As and no two equal Bs - which is one specific case.
                        So I will assume that no two As should be equal and no two Bs should be equal.



                        Some examples with desired outputs:



                        Example(i): [A:'1',B:'1',A:'2',B:'2',A:'3',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                        Example(ii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                        Example(iii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3'] -> [A:'1',B:'1',A:'2',B:'2']
                        Example(iv): ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2',"A":'2',"B":'2'] -> [A:'1',B:'1',A:'2',B:'2']


                        1. One might be tempted to do arr distinctBy ($.A + $.B). But this would work only in the case of example(ii) because it will distinct by combined A and B. Example(i) and example(iii) will remain unchanged. Example(iv) will transform to ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2'] .

                        2. So to achieve stated goal of no two As are equal and no two Bs are equal, we have to first distinct by 'A', then distinct by 'B' and then find the common overlapping array between the two. So I am using filter and contains to achieve this like below:

                        %dw 2.0
                        output application/json
                        var arr = ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2']
                        ---
                        (arr distinctBy $.A) filter ((arr distinctBy $.B) contains $)





                        share|improve this answer





























                          1














                          1










                          1









                          I will give a more general answer that will not only distinct by 'A' but also distinct by 'B' because it is not clear in the question wording if doing distinct by only 'A' is enough. Although your dataweave tries to distinct only by 'A', your example has only two equal As and no two equal Bs - which is one specific case.
                          So I will assume that no two As should be equal and no two Bs should be equal.



                          Some examples with desired outputs:



                          Example(i): [A:'1',B:'1',A:'2',B:'2',A:'3',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(ii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(iii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(iv): ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2',"A":'2',"B":'2'] -> [A:'1',B:'1',A:'2',B:'2']


                          1. One might be tempted to do arr distinctBy ($.A + $.B). But this would work only in the case of example(ii) because it will distinct by combined A and B. Example(i) and example(iii) will remain unchanged. Example(iv) will transform to ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2'] .

                          2. So to achieve stated goal of no two As are equal and no two Bs are equal, we have to first distinct by 'A', then distinct by 'B' and then find the common overlapping array between the two. So I am using filter and contains to achieve this like below:

                          %dw 2.0
                          output application/json
                          var arr = ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2']
                          ---
                          (arr distinctBy $.A) filter ((arr distinctBy $.B) contains $)





                          share|improve this answer















                          I will give a more general answer that will not only distinct by 'A' but also distinct by 'B' because it is not clear in the question wording if doing distinct by only 'A' is enough. Although your dataweave tries to distinct only by 'A', your example has only two equal As and no two equal Bs - which is one specific case.
                          So I will assume that no two As should be equal and no two Bs should be equal.



                          Some examples with desired outputs:



                          Example(i): [A:'1',B:'1',A:'2',B:'2',A:'3',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(ii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'2'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(iii): [A:'1',B:'1',A:'2',B:'2',A:'2',B:'3'] -> [A:'1',B:'1',A:'2',B:'2']
                          Example(iv): ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2',"A":'2',"B":'2'] -> [A:'1',B:'1',A:'2',B:'2']


                          1. One might be tempted to do arr distinctBy ($.A + $.B). But this would work only in the case of example(ii) because it will distinct by combined A and B. Example(i) and example(iii) will remain unchanged. Example(iv) will transform to ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2'] .

                          2. So to achieve stated goal of no two As are equal and no two Bs are equal, we have to first distinct by 'A', then distinct by 'B' and then find the common overlapping array between the two. So I am using filter and contains to achieve this like below:

                          %dw 2.0
                          output application/json
                          var arr = ["A":'1',"B":'1',"A":'2',"B":'2',"A":'3',"B":'2']
                          ---
                          (arr distinctBy $.A) filter ((arr distinctBy $.B) contains $)






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 29 at 6:19

























                          answered Mar 29 at 6:05









                          vikram92vikram92

                          797 bronze badges




                          797 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387628%2fmule-4-dw2-array-of-objects-distinct-by-object-field%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript