How to union to immutable.js list of objects (with id prop)









up vote
0
down vote

favorite












I want to replace / add new values in an immutable list like:



const old = [
id: 1, value: 'foo' ,
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
]

const new = [
id: 1, value: 'quux' , // update
id: 4, value: 'asd' , // push

]

union == [
id: 1, value: 'quux' , // updated
id: 3, value: 'bar' ,
id: 2, value: 'baz' ,
id: 4, value: 'asd' , // new
]


I can use any data type if lists don't have this functionality but something has



If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method










share|improve this question

























    up vote
    0
    down vote

    favorite












    I want to replace / add new values in an immutable list like:



    const old = [
    id: 1, value: 'foo' ,
    id: 3, value: 'bar' ,
    id: 2, value: 'baz' ,
    ]

    const new = [
    id: 1, value: 'quux' , // update
    id: 4, value: 'asd' , // push

    ]

    union == [
    id: 1, value: 'quux' , // updated
    id: 3, value: 'bar' ,
    id: 2, value: 'baz' ,
    id: 4, value: 'asd' , // new
    ]


    I can use any data type if lists don't have this functionality but something has



    If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to replace / add new values in an immutable list like:



      const old = [
      id: 1, value: 'foo' ,
      id: 3, value: 'bar' ,
      id: 2, value: 'baz' ,
      ]

      const new = [
      id: 1, value: 'quux' , // update
      id: 4, value: 'asd' , // push

      ]

      union == [
      id: 1, value: 'quux' , // updated
      id: 3, value: 'bar' ,
      id: 2, value: 'baz' ,
      id: 4, value: 'asd' , // new
      ]


      I can use any data type if lists don't have this functionality but something has



      If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method










      share|improve this question













      I want to replace / add new values in an immutable list like:



      const old = [
      id: 1, value: 'foo' ,
      id: 3, value: 'bar' ,
      id: 2, value: 'baz' ,
      ]

      const new = [
      id: 1, value: 'quux' , // update
      id: 4, value: 'asd' , // push

      ]

      union == [
      id: 1, value: 'quux' , // updated
      id: 3, value: 'bar' ,
      id: 2, value: 'baz' ,
      id: 4, value: 'asd' , // new
      ]


      I can use any data type if lists don't have this functionality but something has



      If this isn't built in immutable.js just say that, i already wrote a script for this, i'm just missing the built in method







      javascript immutable.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 13:14









      tomitheninja

      63




      63






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Algorithms for union



          First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;



          index.js



          const old = [
          id: 1, value: 'foo' ,
          id: 3, value: 'bar' ,
          id: 2, value: 'baz' ,
          ];

          const newa = [
          id: 1, value: 'quux' , // update
          id: 4, value: 'asd' , // push

          ];

          function merged(input,filterMap)
          var output = ;
          input.forEach(function(eachRow)
          if(filterMap.hasOwnProperty(eachRow.id))
          output.push(Object.assign(eachRow,filterMap[eachRow.id]));
          delete filterMap[eachRow.id];
          else
          output.push(eachRow);

          );

          if(Object.keys(filterMap).length > 0)
          output = output.concat(Object.values(filterMap));

          return output;


          function parseData(first,second)
          var mapFirst = ,
          mapSecond = ;
          var output = ;
          first.forEach(function(eachRow)
          mapFirst[eachRow.id] = eachRow;
          );

          second.forEach(function(eachRow)
          mapSecond[eachRow.id] = eachRow;
          );

          if(first.length > second.length)
          return merged(first,mapSecond);
          else
          return merged(second,mapFirst);



          console.log(parseData(old,newa));


          Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/






          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',
            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%2f53239305%2fhow-to-union-to-immutable-js-list-of-objects-with-id-prop%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Algorithms for union



            First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;



            index.js



            const old = [
            id: 1, value: 'foo' ,
            id: 3, value: 'bar' ,
            id: 2, value: 'baz' ,
            ];

            const newa = [
            id: 1, value: 'quux' , // update
            id: 4, value: 'asd' , // push

            ];

            function merged(input,filterMap)
            var output = ;
            input.forEach(function(eachRow)
            if(filterMap.hasOwnProperty(eachRow.id))
            output.push(Object.assign(eachRow,filterMap[eachRow.id]));
            delete filterMap[eachRow.id];
            else
            output.push(eachRow);

            );

            if(Object.keys(filterMap).length > 0)
            output = output.concat(Object.values(filterMap));

            return output;


            function parseData(first,second)
            var mapFirst = ,
            mapSecond = ;
            var output = ;
            first.forEach(function(eachRow)
            mapFirst[eachRow.id] = eachRow;
            );

            second.forEach(function(eachRow)
            mapSecond[eachRow.id] = eachRow;
            );

            if(first.length > second.length)
            return merged(first,mapSecond);
            else
            return merged(second,mapFirst);



            console.log(parseData(old,newa));


            Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/






            share|improve this answer
























              up vote
              0
              down vote













              Algorithms for union



              First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;



              index.js



              const old = [
              id: 1, value: 'foo' ,
              id: 3, value: 'bar' ,
              id: 2, value: 'baz' ,
              ];

              const newa = [
              id: 1, value: 'quux' , // update
              id: 4, value: 'asd' , // push

              ];

              function merged(input,filterMap)
              var output = ;
              input.forEach(function(eachRow)
              if(filterMap.hasOwnProperty(eachRow.id))
              output.push(Object.assign(eachRow,filterMap[eachRow.id]));
              delete filterMap[eachRow.id];
              else
              output.push(eachRow);

              );

              if(Object.keys(filterMap).length > 0)
              output = output.concat(Object.values(filterMap));

              return output;


              function parseData(first,second)
              var mapFirst = ,
              mapSecond = ;
              var output = ;
              first.forEach(function(eachRow)
              mapFirst[eachRow.id] = eachRow;
              );

              second.forEach(function(eachRow)
              mapSecond[eachRow.id] = eachRow;
              );

              if(first.length > second.length)
              return merged(first,mapSecond);
              else
              return merged(second,mapFirst);



              console.log(parseData(old,newa));


              Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                Algorithms for union



                First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;



                index.js



                const old = [
                id: 1, value: 'foo' ,
                id: 3, value: 'bar' ,
                id: 2, value: 'baz' ,
                ];

                const newa = [
                id: 1, value: 'quux' , // update
                id: 4, value: 'asd' , // push

                ];

                function merged(input,filterMap)
                var output = ;
                input.forEach(function(eachRow)
                if(filterMap.hasOwnProperty(eachRow.id))
                output.push(Object.assign(eachRow,filterMap[eachRow.id]));
                delete filterMap[eachRow.id];
                else
                output.push(eachRow);

                );

                if(Object.keys(filterMap).length > 0)
                output = output.concat(Object.values(filterMap));

                return output;


                function parseData(first,second)
                var mapFirst = ,
                mapSecond = ;
                var output = ;
                first.forEach(function(eachRow)
                mapFirst[eachRow.id] = eachRow;
                );

                second.forEach(function(eachRow)
                mapSecond[eachRow.id] = eachRow;
                );

                if(first.length > second.length)
                return merged(first,mapSecond);
                else
                return merged(second,mapFirst);



                console.log(parseData(old,newa));


                Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/






                share|improve this answer












                Algorithms for union



                First you have to maintain two map with key as id and value as object then check for length of array which is of bigger size and pass the bigger size array with small size map to merged function there you can iterate over the array and check if it's exists in the map if yes then update the object and delete that row from map otherwise add the object into output. After the for loop complete check if map has element present then push all the values from map into output array and return;



                index.js



                const old = [
                id: 1, value: 'foo' ,
                id: 3, value: 'bar' ,
                id: 2, value: 'baz' ,
                ];

                const newa = [
                id: 1, value: 'quux' , // update
                id: 4, value: 'asd' , // push

                ];

                function merged(input,filterMap)
                var output = ;
                input.forEach(function(eachRow)
                if(filterMap.hasOwnProperty(eachRow.id))
                output.push(Object.assign(eachRow,filterMap[eachRow.id]));
                delete filterMap[eachRow.id];
                else
                output.push(eachRow);

                );

                if(Object.keys(filterMap).length > 0)
                output = output.concat(Object.values(filterMap));

                return output;


                function parseData(first,second)
                var mapFirst = ,
                mapSecond = ;
                var output = ;
                first.forEach(function(eachRow)
                mapFirst[eachRow.id] = eachRow;
                );

                second.forEach(function(eachRow)
                mapSecond[eachRow.id] = eachRow;
                );

                if(first.length > second.length)
                return merged(first,mapSecond);
                else
                return merged(second,mapFirst);



                console.log(parseData(old,newa));


                Working jsFiddle demo - https://jsfiddle.net/qz25hnmf/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 14:06









                front_end_dev

                1,310411




                1,310411



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239305%2fhow-to-union-to-immutable-js-list-of-objects-with-id-prop%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    Evgeni Malkin