How to add list to list in Immutable.js?












0














What is the best way to add List to List in Immutable.js?



concat method is working, but another way is not working.



const a = fromJS([  
{
comment: 'aaaa',
who: 'a1',
buttonInfo: ['a', 'b', 'c'],
},
{
comment: 'bb',
who: 'a2',
buttonInfo: ['a', 'b', 'c'],
},
]);

const b = fromJS([
{
comment: 'ccc',
who: 'c1',
buttonInfo: ['a', 'b'],
},
{
comment: 'ddd',
who: 'd2',
buttonInfo: ['a''],
},
]);


This is working:



a.concat(b)


But this is not working:



[...a ,...b]

// or

b.map(v => {
a.push(v);
})









share|improve this question





























    0














    What is the best way to add List to List in Immutable.js?



    concat method is working, but another way is not working.



    const a = fromJS([  
    {
    comment: 'aaaa',
    who: 'a1',
    buttonInfo: ['a', 'b', 'c'],
    },
    {
    comment: 'bb',
    who: 'a2',
    buttonInfo: ['a', 'b', 'c'],
    },
    ]);

    const b = fromJS([
    {
    comment: 'ccc',
    who: 'c1',
    buttonInfo: ['a', 'b'],
    },
    {
    comment: 'ddd',
    who: 'd2',
    buttonInfo: ['a''],
    },
    ]);


    This is working:



    a.concat(b)


    But this is not working:



    [...a ,...b]

    // or

    b.map(v => {
    a.push(v);
    })









    share|improve this question



























      0












      0








      0







      What is the best way to add List to List in Immutable.js?



      concat method is working, but another way is not working.



      const a = fromJS([  
      {
      comment: 'aaaa',
      who: 'a1',
      buttonInfo: ['a', 'b', 'c'],
      },
      {
      comment: 'bb',
      who: 'a2',
      buttonInfo: ['a', 'b', 'c'],
      },
      ]);

      const b = fromJS([
      {
      comment: 'ccc',
      who: 'c1',
      buttonInfo: ['a', 'b'],
      },
      {
      comment: 'ddd',
      who: 'd2',
      buttonInfo: ['a''],
      },
      ]);


      This is working:



      a.concat(b)


      But this is not working:



      [...a ,...b]

      // or

      b.map(v => {
      a.push(v);
      })









      share|improve this question















      What is the best way to add List to List in Immutable.js?



      concat method is working, but another way is not working.



      const a = fromJS([  
      {
      comment: 'aaaa',
      who: 'a1',
      buttonInfo: ['a', 'b', 'c'],
      },
      {
      comment: 'bb',
      who: 'a2',
      buttonInfo: ['a', 'b', 'c'],
      },
      ]);

      const b = fromJS([
      {
      comment: 'ccc',
      who: 'c1',
      buttonInfo: ['a', 'b'],
      },
      {
      comment: 'ddd',
      who: 'd2',
      buttonInfo: ['a''],
      },
      ]);


      This is working:



      a.concat(b)


      But this is not working:



      [...a ,...b]

      // or

      b.map(v => {
      a.push(v);
      })






      javascript reactjs immutable.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 11:38









      soroush chehresa

      1,295313




      1,295313










      asked Nov 12 '18 at 10:53









      kekeeekekeee

      1215




      1215
























          3 Answers
          3






          active

          oldest

          votes


















          1














          you can use concat method as it said in doc:






          const list1 = List([ 1, 2, 3 ]);
          const list2 = List([ 4, 5, 6 ]);
          const array = [ 7, 8, 9 ];
          const list3 = list1.concat(list2, array);
          // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]








          share|improve this answer





















          • concat is best way???
            – kekeee
            Nov 12 '18 at 11:20






          • 1




            depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
            – igorves
            Nov 12 '18 at 11:24





















          0














          An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.



          Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like



          b.map(v => {
          a = a.push(v);
          })



          P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case







          share|improve this answer





























            0














            For add List to List in Immutable.js, you can use merge method.



            Example:



            const a = fromJS(
            [
            {
            comment: 'aaaa',
            who: 'a1',
            buttonInfo: ['a', 'b', 'c'],
            },
            {
            comment: 'bb',
            who: 'a2',
            buttonInfo: ['a', 'b', 'c'],
            },
            ]
            );

            const b = fromJS(
            [
            {
            comment: 'ccc',
            who: 'c1',
            buttonInfo: ['a', 'b'],
            },
            {
            comment: 'ddd',
            who: 'd2',
            buttonInfo: ['a''],
            },
            ]
            );


            a.merge(b);





            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%2f53260616%2fhow-to-add-list-to-list-in-immutable-js%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              you can use concat method as it said in doc:






              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]








              share|improve this answer





















              • concat is best way???
                – kekeee
                Nov 12 '18 at 11:20






              • 1




                depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
                – igorves
                Nov 12 '18 at 11:24


















              1














              you can use concat method as it said in doc:






              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]








              share|improve this answer





















              • concat is best way???
                – kekeee
                Nov 12 '18 at 11:20






              • 1




                depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
                – igorves
                Nov 12 '18 at 11:24
















              1












              1








              1






              you can use concat method as it said in doc:






              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]








              share|improve this answer












              you can use concat method as it said in doc:






              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]








              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]





              const list1 = List([ 1, 2, 3 ]);
              const list2 = List([ 4, 5, 6 ]);
              const array = [ 7, 8, 9 ];
              const list3 = list1.concat(list2, array);
              // List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 '18 at 11:08









              igorvesigorves

              16116




              16116












              • concat is best way???
                – kekeee
                Nov 12 '18 at 11:20






              • 1




                depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
                – igorves
                Nov 12 '18 at 11:24




















              • concat is best way???
                – kekeee
                Nov 12 '18 at 11:20






              • 1




                depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
                – igorves
                Nov 12 '18 at 11:24


















              concat is best way???
              – kekeee
              Nov 12 '18 at 11:20




              concat is best way???
              – kekeee
              Nov 12 '18 at 11:20




              1




              1




              depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
              – igorves
              Nov 12 '18 at 11:24






              depends what do you want. You can use both merge and concat cause: "Immutable.js can treat any JavaScript Array or Object as a Collection". Basically you can also check: github.com/rtfeldman/seamless-immutable
              – igorves
              Nov 12 '18 at 11:24















              0














              An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.



              Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like



              b.map(v => {
              a = a.push(v);
              })



              P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case







              share|improve this answer


























                0














                An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.



                Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like



                b.map(v => {
                a = a.push(v);
                })



                P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case







                share|improve this answer
























                  0












                  0








                  0






                  An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.



                  Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like



                  b.map(v => {
                  a = a.push(v);
                  })



                  P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case







                  share|improve this answer












                  An ImmutableJS list has a method named concat whose behavior is the same as a normal javascript array. However, you cannot use spread syntax for an Immutable array.



                  Also the syntax for push is different from the normal array, push like concat with Immutable List returns a new list, your map method will look like



                  b.map(v => {
                  a = a.push(v);
                  })



                  P.S. Using the above method though will mutate your array a. You must create a new List and then push both the array contents into it if you want to use push. However concat is the best way for your case








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 11:00









                  Shubham KhatriShubham Khatri

                  79.1k1494130




                  79.1k1494130























                      0














                      For add List to List in Immutable.js, you can use merge method.



                      Example:



                      const a = fromJS(
                      [
                      {
                      comment: 'aaaa',
                      who: 'a1',
                      buttonInfo: ['a', 'b', 'c'],
                      },
                      {
                      comment: 'bb',
                      who: 'a2',
                      buttonInfo: ['a', 'b', 'c'],
                      },
                      ]
                      );

                      const b = fromJS(
                      [
                      {
                      comment: 'ccc',
                      who: 'c1',
                      buttonInfo: ['a', 'b'],
                      },
                      {
                      comment: 'ddd',
                      who: 'd2',
                      buttonInfo: ['a''],
                      },
                      ]
                      );


                      a.merge(b);





                      share|improve this answer


























                        0














                        For add List to List in Immutable.js, you can use merge method.



                        Example:



                        const a = fromJS(
                        [
                        {
                        comment: 'aaaa',
                        who: 'a1',
                        buttonInfo: ['a', 'b', 'c'],
                        },
                        {
                        comment: 'bb',
                        who: 'a2',
                        buttonInfo: ['a', 'b', 'c'],
                        },
                        ]
                        );

                        const b = fromJS(
                        [
                        {
                        comment: 'ccc',
                        who: 'c1',
                        buttonInfo: ['a', 'b'],
                        },
                        {
                        comment: 'ddd',
                        who: 'd2',
                        buttonInfo: ['a''],
                        },
                        ]
                        );


                        a.merge(b);





                        share|improve this answer
























                          0












                          0








                          0






                          For add List to List in Immutable.js, you can use merge method.



                          Example:



                          const a = fromJS(
                          [
                          {
                          comment: 'aaaa',
                          who: 'a1',
                          buttonInfo: ['a', 'b', 'c'],
                          },
                          {
                          comment: 'bb',
                          who: 'a2',
                          buttonInfo: ['a', 'b', 'c'],
                          },
                          ]
                          );

                          const b = fromJS(
                          [
                          {
                          comment: 'ccc',
                          who: 'c1',
                          buttonInfo: ['a', 'b'],
                          },
                          {
                          comment: 'ddd',
                          who: 'd2',
                          buttonInfo: ['a''],
                          },
                          ]
                          );


                          a.merge(b);





                          share|improve this answer












                          For add List to List in Immutable.js, you can use merge method.



                          Example:



                          const a = fromJS(
                          [
                          {
                          comment: 'aaaa',
                          who: 'a1',
                          buttonInfo: ['a', 'b', 'c'],
                          },
                          {
                          comment: 'bb',
                          who: 'a2',
                          buttonInfo: ['a', 'b', 'c'],
                          },
                          ]
                          );

                          const b = fromJS(
                          [
                          {
                          comment: 'ccc',
                          who: 'c1',
                          buttonInfo: ['a', 'b'],
                          },
                          {
                          comment: 'ddd',
                          who: 'd2',
                          buttonInfo: ['a''],
                          },
                          ]
                          );


                          a.merge(b);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 '18 at 11:16









                          soroush chehresasoroush chehresa

                          1,295313




                          1,295313






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53260616%2fhow-to-add-list-to-list-in-immutable-js%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

                              Full-time equivalent

                              さくらももこ

                              13 indicted, 8 arrested in Calif. drug cartel investigation