Destructuring for get the first property of an object?












-1















For arrays, we can define the properties depending on it's indexes like:






const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





I'm just wondering if there's a possible solution to do it's reverse with objects like:






const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"












share|improve this question




















  • 4





    Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

    – jonrsharpe
    Nov 16 '18 at 11:22








  • 1





    Keys in object don't have an order You can use Map or Array only

    – HMR
    Nov 16 '18 at 11:23













  • @jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

    – Gergő Horváth
    Nov 16 '18 at 11:27








  • 2





    No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

    – jonrsharpe
    Nov 16 '18 at 11:29






  • 1





    please add the wanted result as well.

    – Nina Scholz
    Nov 16 '18 at 11:29
















-1















For arrays, we can define the properties depending on it's indexes like:






const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





I'm just wondering if there's a possible solution to do it's reverse with objects like:






const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"












share|improve this question




















  • 4





    Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

    – jonrsharpe
    Nov 16 '18 at 11:22








  • 1





    Keys in object don't have an order You can use Map or Array only

    – HMR
    Nov 16 '18 at 11:23













  • @jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

    – Gergő Horváth
    Nov 16 '18 at 11:27








  • 2





    No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

    – jonrsharpe
    Nov 16 '18 at 11:29






  • 1





    please add the wanted result as well.

    – Nina Scholz
    Nov 16 '18 at 11:29














-1












-1








-1








For arrays, we can define the properties depending on it's indexes like:






const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





I'm just wondering if there's a possible solution to do it's reverse with objects like:






const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"












share|improve this question
















For arrays, we can define the properties depending on it's indexes like:






const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





I'm just wondering if there's a possible solution to do it's reverse with objects like:






const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"








const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)





const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"





const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:32







Gergő Horváth

















asked Nov 16 '18 at 11:20









Gergő HorváthGergő Horváth

566115




566115








  • 4





    Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

    – jonrsharpe
    Nov 16 '18 at 11:22








  • 1





    Keys in object don't have an order You can use Map or Array only

    – HMR
    Nov 16 '18 at 11:23













  • @jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

    – Gergő Horváth
    Nov 16 '18 at 11:27








  • 2





    No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

    – jonrsharpe
    Nov 16 '18 at 11:29






  • 1





    please add the wanted result as well.

    – Nina Scholz
    Nov 16 '18 at 11:29














  • 4





    Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

    – jonrsharpe
    Nov 16 '18 at 11:22








  • 1





    Keys in object don't have an order You can use Map or Array only

    – HMR
    Nov 16 '18 at 11:23













  • @jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

    – Gergő Horváth
    Nov 16 '18 at 11:27








  • 2





    No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

    – jonrsharpe
    Nov 16 '18 at 11:29






  • 1





    please add the wanted result as well.

    – Nina Scholz
    Nov 16 '18 at 11:29








4




4





Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

– jonrsharpe
Nov 16 '18 at 11:22







Objects don't have a "first", they aren't ordered. Also 0, 1 and 2 aren't valid identifiers. Neither of your snippets works, and it's not clear what you'd expect as an outcome.

– jonrsharpe
Nov 16 '18 at 11:22






1




1





Keys in object don't have an order You can use Map or Array only

– HMR
Nov 16 '18 at 11:23







Keys in object don't have an order You can use Map or Array only

– HMR
Nov 16 '18 at 11:23















@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

– Gergő Horváth
Nov 16 '18 at 11:27







@jonrsharpe try the first snippet. It will work. What I expect as an outcome is quite clear in the comment as I think. Secondly, the "like" word shows that it should work like in the example.

– Gergő Horváth
Nov 16 '18 at 11:27






2




2





No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

– jonrsharpe
Nov 16 '18 at 11:29





No it doesn't, I get a SyntaxError (e.g. missing variable name or unexpected string, depending on the implementation). And it might be clear to you, but if you're asking here it also needs to be clear to other people.

– jonrsharpe
Nov 16 '18 at 11:29




1




1





please add the wanted result as well.

– Nina Scholz
Nov 16 '18 at 11:29





please add the wanted result as well.

– Nina Scholz
Nov 16 '18 at 11:29












4 Answers
4






active

oldest

votes


















0














You could take the values and assign this to an array for destructuring.



The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.






const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);

console.log(first, second, third);





For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.






const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);

console.log(foo);








share|improve this answer































    1














    You do it like this for objects:



    const obj = {foo: 123, bar: 'str'}

    const {foo, bar} = obj





    share|improve this answer































      1














      It isn't.



      Objects are not designed to be ordered, so there isn't a first property per se.



      You could convert an object into an array of its values first …






      const obj = {
      first: "a",
      second: "b",
      third: "c"
      }
      const array = Object.values(obj);
      const [foo, bar, baz] = array;
      console.log({
      foo,
      bar,
      baz
      });





      … but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.






      share|improve this answer

































        1














        Try this:



        const obj = {first: "a", second: "b", third: "c"}
        const indexes = [0, 1, 2]
        indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]





        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%2f53336844%2fdestructuring-for-get-the-first-property-of-an-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You could take the values and assign this to an array for destructuring.



          The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.






          const
          object = { first: "a", second: "b", third: "c" },
          [first, second, third] = Object.values(object);

          console.log(first, second, third);





          For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.






          const
          object = { first: "a", second: "b", third: "c" },
          { 2: foo } = Object.values(object);

          console.log(foo);








          share|improve this answer




























            0














            You could take the values and assign this to an array for destructuring.



            The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.






            const
            object = { first: "a", second: "b", third: "c" },
            [first, second, third] = Object.values(object);

            console.log(first, second, third);





            For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.






            const
            object = { first: "a", second: "b", third: "c" },
            { 2: foo } = Object.values(object);

            console.log(foo);








            share|improve this answer


























              0












              0








              0







              You could take the values and assign this to an array for destructuring.



              The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.






              const
              object = { first: "a", second: "b", third: "c" },
              [first, second, third] = Object.values(object);

              console.log(first, second, third);





              For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.






              const
              object = { first: "a", second: "b", third: "c" },
              { 2: foo } = Object.values(object);

              console.log(foo);








              share|improve this answer













              You could take the values and assign this to an array for destructuring.



              The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.






              const
              object = { first: "a", second: "b", third: "c" },
              [first, second, third] = Object.values(object);

              console.log(first, second, third);





              For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.






              const
              object = { first: "a", second: "b", third: "c" },
              { 2: foo } = Object.values(object);

              console.log(foo);








              const
              object = { first: "a", second: "b", third: "c" },
              [first, second, third] = Object.values(object);

              console.log(first, second, third);





              const
              object = { first: "a", second: "b", third: "c" },
              [first, second, third] = Object.values(object);

              console.log(first, second, third);





              const
              object = { first: "a", second: "b", third: "c" },
              { 2: foo } = Object.values(object);

              console.log(foo);





              const
              object = { first: "a", second: "b", third: "c" },
              { 2: foo } = Object.values(object);

              console.log(foo);






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 11:39









              Nina ScholzNina Scholz

              195k15107179




              195k15107179

























                  1














                  You do it like this for objects:



                  const obj = {foo: 123, bar: 'str'}

                  const {foo, bar} = obj





                  share|improve this answer




























                    1














                    You do it like this for objects:



                    const obj = {foo: 123, bar: 'str'}

                    const {foo, bar} = obj





                    share|improve this answer


























                      1












                      1








                      1







                      You do it like this for objects:



                      const obj = {foo: 123, bar: 'str'}

                      const {foo, bar} = obj





                      share|improve this answer













                      You do it like this for objects:



                      const obj = {foo: 123, bar: 'str'}

                      const {foo, bar} = obj






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 11:34









                      Nurbol AlpysbayevNurbol Alpysbayev

                      4,8241533




                      4,8241533























                          1














                          It isn't.



                          Objects are not designed to be ordered, so there isn't a first property per se.



                          You could convert an object into an array of its values first …






                          const obj = {
                          first: "a",
                          second: "b",
                          third: "c"
                          }
                          const array = Object.values(obj);
                          const [foo, bar, baz] = array;
                          console.log({
                          foo,
                          bar,
                          baz
                          });





                          … but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.






                          share|improve this answer






























                            1














                            It isn't.



                            Objects are not designed to be ordered, so there isn't a first property per se.



                            You could convert an object into an array of its values first …






                            const obj = {
                            first: "a",
                            second: "b",
                            third: "c"
                            }
                            const array = Object.values(obj);
                            const [foo, bar, baz] = array;
                            console.log({
                            foo,
                            bar,
                            baz
                            });





                            … but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.






                            share|improve this answer




























                              1












                              1








                              1







                              It isn't.



                              Objects are not designed to be ordered, so there isn't a first property per se.



                              You could convert an object into an array of its values first …






                              const obj = {
                              first: "a",
                              second: "b",
                              third: "c"
                              }
                              const array = Object.values(obj);
                              const [foo, bar, baz] = array;
                              console.log({
                              foo,
                              bar,
                              baz
                              });





                              … but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.






                              share|improve this answer















                              It isn't.



                              Objects are not designed to be ordered, so there isn't a first property per se.



                              You could convert an object into an array of its values first …






                              const obj = {
                              first: "a",
                              second: "b",
                              third: "c"
                              }
                              const array = Object.values(obj);
                              const [foo, bar, baz] = array;
                              console.log({
                              foo,
                              bar,
                              baz
                              });





                              … but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.






                              const obj = {
                              first: "a",
                              second: "b",
                              third: "c"
                              }
                              const array = Object.values(obj);
                              const [foo, bar, baz] = array;
                              console.log({
                              foo,
                              bar,
                              baz
                              });





                              const obj = {
                              first: "a",
                              second: "b",
                              third: "c"
                              }
                              const array = Object.values(obj);
                              const [foo, bar, baz] = array;
                              console.log({
                              foo,
                              bar,
                              baz
                              });






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 16 '18 at 11:38

























                              answered Nov 16 '18 at 11:22









                              QuentinQuentin

                              657k728931056




                              657k728931056























                                  1














                                  Try this:



                                  const obj = {first: "a", second: "b", third: "c"}
                                  const indexes = [0, 1, 2]
                                  indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]





                                  share|improve this answer




























                                    1














                                    Try this:



                                    const obj = {first: "a", second: "b", third: "c"}
                                    const indexes = [0, 1, 2]
                                    indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Try this:



                                      const obj = {first: "a", second: "b", third: "c"}
                                      const indexes = [0, 1, 2]
                                      indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]





                                      share|improve this answer













                                      Try this:



                                      const obj = {first: "a", second: "b", third: "c"}
                                      const indexes = [0, 1, 2]
                                      indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 11:51









                                      javimovijavimovi

                                      318110




                                      318110






























                                          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%2f53336844%2fdestructuring-for-get-the-first-property-of-an-object%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

                                          Bressuire

                                          Vorschmack

                                          Quarantine