Why can't Javascript spread operator be used after object key?











up vote
2
down vote

favorite












I have the following code:



const array = [{
a: 'a',
b: 'b'
}];

console.log(...array);

const store = {
obj: ...array
}


console.log will print the results just fine. However, when trying to set the key of store I get a Parsing error: Unexpected token.



Isn't ...array a valid object to assign to the obj key of store?










share|improve this question


























    up vote
    2
    down vote

    favorite












    I have the following code:



    const array = [{
    a: 'a',
    b: 'b'
    }];

    console.log(...array);

    const store = {
    obj: ...array
    }


    console.log will print the results just fine. However, when trying to set the key of store I get a Parsing error: Unexpected token.



    Isn't ...array a valid object to assign to the obj key of store?










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have the following code:



      const array = [{
      a: 'a',
      b: 'b'
      }];

      console.log(...array);

      const store = {
      obj: ...array
      }


      console.log will print the results just fine. However, when trying to set the key of store I get a Parsing error: Unexpected token.



      Isn't ...array a valid object to assign to the obj key of store?










      share|improve this question













      I have the following code:



      const array = [{
      a: 'a',
      b: 'b'
      }];

      console.log(...array);

      const store = {
      obj: ...array
      }


      console.log will print the results just fine. However, when trying to set the key of store I get a Parsing error: Unexpected token.



      Isn't ...array a valid object to assign to the obj key of store?







      javascript spread






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 18:20









      Yos

      332413




      332413
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          ... spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array} or obj : [...array]






          const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

          console.log(...array);

          const store = {
          obj: {...array},
          obj1: [...array]
          };

          console.log(store);








          share|improve this answer




























            up vote
            1
            down vote













            The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.




            Spread Syntax



            Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.







            const array = [0, 1, 2]
            const store = {
            obj: [...array] // <-- the array is being spreded into an array.
            }

            console.log(store)








            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%2f53242038%2fwhy-cant-javascript-spread-operator-be-used-after-object-key%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








              up vote
              5
              down vote



              accepted










              ... spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array} or obj : [...array]






              const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

              console.log(...array);

              const store = {
              obj: {...array},
              obj1: [...array]
              };

              console.log(store);








              share|improve this answer

























                up vote
                5
                down vote



                accepted










                ... spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array} or obj : [...array]






                const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

                console.log(...array);

                const store = {
                obj: {...array},
                obj1: [...array]
                };

                console.log(store);








                share|improve this answer























                  up vote
                  5
                  down vote



                  accepted







                  up vote
                  5
                  down vote



                  accepted






                  ... spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array} or obj : [...array]






                  const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

                  console.log(...array);

                  const store = {
                  obj: {...array},
                  obj1: [...array]
                  };

                  console.log(store);








                  share|improve this answer












                  ... spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array} or obj : [...array]






                  const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

                  console.log(...array);

                  const store = {
                  obj: {...array},
                  obj1: [...array]
                  };

                  console.log(store);








                  const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

                  console.log(...array);

                  const store = {
                  obj: {...array},
                  obj1: [...array]
                  };

                  console.log(store);





                  const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];

                  console.log(...array);

                  const store = {
                  obj: {...array},
                  obj1: [...array]
                  };

                  console.log(store);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 18:25









                  Nikhil Aggarwal

                  23.1k32647




                  23.1k32647
























                      up vote
                      1
                      down vote













                      The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.




                      Spread Syntax



                      Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.







                      const array = [0, 1, 2]
                      const store = {
                      obj: [...array] // <-- the array is being spreded into an array.
                      }

                      console.log(store)








                      share|improve this answer

























                        up vote
                        1
                        down vote













                        The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.




                        Spread Syntax



                        Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.







                        const array = [0, 1, 2]
                        const store = {
                        obj: [...array] // <-- the array is being spreded into an array.
                        }

                        console.log(store)








                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.




                          Spread Syntax



                          Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.







                          const array = [0, 1, 2]
                          const store = {
                          obj: [...array] // <-- the array is being spreded into an array.
                          }

                          console.log(store)








                          share|improve this answer












                          The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.




                          Spread Syntax



                          Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.







                          const array = [0, 1, 2]
                          const store = {
                          obj: [...array] // <-- the array is being spreded into an array.
                          }

                          console.log(store)








                          const array = [0, 1, 2]
                          const store = {
                          obj: [...array] // <-- the array is being spreded into an array.
                          }

                          console.log(store)





                          const array = [0, 1, 2]
                          const store = {
                          obj: [...array] // <-- the array is being spreded into an array.
                          }

                          console.log(store)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 18:30









                          Ele

                          22.2k42044




                          22.2k42044






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242038%2fwhy-cant-javascript-spread-operator-be-used-after-object-key%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

                              Florida Star v. B. J. F.

                              Error while running script in elastic search , gateway timeout

                              Adding quotations to stringified JSON object values