Convert array of objects to array of arrays?









up vote
1
down vote

favorite












Given is an json object like this:



var items = [
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/'
,
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/'
,
title: 'sample 3',
image: 'http://www.lorempixel.com/400/300/'
,
title: 'sample 4',
image: 'http://www.lorempixel.com/600/600/'
,
title: 'sample 5',
image: 'http://www.lorempixel.com/400/310/'
,
title: 'sample 6',
image: 'http://www.lorempixel.com/410/300/'
,
title: 'sample 7',
image: 'http://www.lorempixel.com/500/300/'
,
title: 'sample 8',
image: 'http://www.lorempixel.com/300/300/'
,
title: 'sample 9',
image: 'http://www.lorempixel.com/450/320/'
,
title: 'sample 10',
image: 'http://www.lorempixel.com/500/400/'
];


I Want to get an array like this.



[['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]


I tried something like this:



const rows = ;
for (let i = 0; i < items.length; i++)
const element = items[i];
rows[i] = element.title;
for (let j = i; j < items.image; j++)
const el = response[j];
rows[j] = el.image;




I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?










share|improve this question



























    up vote
    1
    down vote

    favorite












    Given is an json object like this:



    var items = [
    title: 'sample 1',
    image: 'http://www.lorempixel.com/700/600/'
    ,
    title: 'sample 2',
    image: 'http://www.lorempixel.com/900/1200/'
    ,
    title: 'sample 3',
    image: 'http://www.lorempixel.com/400/300/'
    ,
    title: 'sample 4',
    image: 'http://www.lorempixel.com/600/600/'
    ,
    title: 'sample 5',
    image: 'http://www.lorempixel.com/400/310/'
    ,
    title: 'sample 6',
    image: 'http://www.lorempixel.com/410/300/'
    ,
    title: 'sample 7',
    image: 'http://www.lorempixel.com/500/300/'
    ,
    title: 'sample 8',
    image: 'http://www.lorempixel.com/300/300/'
    ,
    title: 'sample 9',
    image: 'http://www.lorempixel.com/450/320/'
    ,
    title: 'sample 10',
    image: 'http://www.lorempixel.com/500/400/'
    ];


    I Want to get an array like this.



    [['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]


    I tried something like this:



    const rows = ;
    for (let i = 0; i < items.length; i++)
    const element = items[i];
    rows[i] = element.title;
    for (let j = i; j < items.image; j++)
    const el = response[j];
    rows[j] = el.image;




    I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Given is an json object like this:



      var items = [
      title: 'sample 1',
      image: 'http://www.lorempixel.com/700/600/'
      ,
      title: 'sample 2',
      image: 'http://www.lorempixel.com/900/1200/'
      ,
      title: 'sample 3',
      image: 'http://www.lorempixel.com/400/300/'
      ,
      title: 'sample 4',
      image: 'http://www.lorempixel.com/600/600/'
      ,
      title: 'sample 5',
      image: 'http://www.lorempixel.com/400/310/'
      ,
      title: 'sample 6',
      image: 'http://www.lorempixel.com/410/300/'
      ,
      title: 'sample 7',
      image: 'http://www.lorempixel.com/500/300/'
      ,
      title: 'sample 8',
      image: 'http://www.lorempixel.com/300/300/'
      ,
      title: 'sample 9',
      image: 'http://www.lorempixel.com/450/320/'
      ,
      title: 'sample 10',
      image: 'http://www.lorempixel.com/500/400/'
      ];


      I Want to get an array like this.



      [['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]


      I tried something like this:



      const rows = ;
      for (let i = 0; i < items.length; i++)
      const element = items[i];
      rows[i] = element.title;
      for (let j = i; j < items.image; j++)
      const el = response[j];
      rows[j] = el.image;




      I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?










      share|improve this question















      Given is an json object like this:



      var items = [
      title: 'sample 1',
      image: 'http://www.lorempixel.com/700/600/'
      ,
      title: 'sample 2',
      image: 'http://www.lorempixel.com/900/1200/'
      ,
      title: 'sample 3',
      image: 'http://www.lorempixel.com/400/300/'
      ,
      title: 'sample 4',
      image: 'http://www.lorempixel.com/600/600/'
      ,
      title: 'sample 5',
      image: 'http://www.lorempixel.com/400/310/'
      ,
      title: 'sample 6',
      image: 'http://www.lorempixel.com/410/300/'
      ,
      title: 'sample 7',
      image: 'http://www.lorempixel.com/500/300/'
      ,
      title: 'sample 8',
      image: 'http://www.lorempixel.com/300/300/'
      ,
      title: 'sample 9',
      image: 'http://www.lorempixel.com/450/320/'
      ,
      title: 'sample 10',
      image: 'http://www.lorempixel.com/500/400/'
      ];


      I Want to get an array like this.



      [['sample 1', 'http://www.lorempixel.com/700/600/'], ['sample 2', 'http://www.lorempixel.com/900/1200/'], ....]


      I tried something like this:



      const rows = ;
      for (let i = 0; i < items.length; i++)
      const element = items[i];
      rows[i] = element.title;
      for (let j = i; j < items.image; j++)
      const el = response[j];
      rows[j] = el.image;




      I get an array containing only all images, i think because i override other values in the second loop. What should be the right way to get both values like described ?







      javascript arrays json object mapping






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 6:46









      Mohammad Usman

      19.2k103653




      19.2k103653










      asked Nov 10 at 6:35









      billyjov

      658314




      658314






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Simply pass Object.values() as callback to .map():






          let data = [
          title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
          title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
          title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
          title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
          title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
          ];

          let result = data.map(Object.values);

          console.log(result);

          .as-console-wrapper max-height: 100% !important; top: 0; 








          share|improve this answer



























            up vote
            1
            down vote













            All you need is a simple .map - extract the appropriate property values from the object, and return an array containing those two values:






            var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

            console.log(
            items.map(( title, image ) => [title, image])
            );








            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%2f53236592%2fconvert-array-of-objects-to-array-of-arrays%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
              2
              down vote



              accepted










              Simply pass Object.values() as callback to .map():






              let data = [
              title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
              title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
              title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
              title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
              title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
              ];

              let result = data.map(Object.values);

              console.log(result);

              .as-console-wrapper max-height: 100% !important; top: 0; 








              share|improve this answer
























                up vote
                2
                down vote



                accepted










                Simply pass Object.values() as callback to .map():






                let data = [
                title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
                title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
                title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
                title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
                title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
                ];

                let result = data.map(Object.values);

                console.log(result);

                .as-console-wrapper max-height: 100% !important; top: 0; 








                share|improve this answer






















                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  Simply pass Object.values() as callback to .map():






                  let data = [
                  title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
                  title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
                  title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
                  title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
                  title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
                  ];

                  let result = data.map(Object.values);

                  console.log(result);

                  .as-console-wrapper max-height: 100% !important; top: 0; 








                  share|improve this answer












                  Simply pass Object.values() as callback to .map():






                  let data = [
                  title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
                  title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
                  title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
                  title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
                  title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
                  ];

                  let result = data.map(Object.values);

                  console.log(result);

                  .as-console-wrapper max-height: 100% !important; top: 0; 








                  let data = [
                  title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
                  title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
                  title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
                  title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
                  title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
                  ];

                  let result = data.map(Object.values);

                  console.log(result);

                  .as-console-wrapper max-height: 100% !important; top: 0; 





                  let data = [
                  title: 'sample 1', image: 'http://www.lorempixel.com/700/600/',
                  title: 'sample 2', image: 'http://www.lorempixel.com/900/1200/',
                  title: 'sample 3', image: 'http://www.lorempixel.com/400/300/',
                  title: 'sample 4', image: 'http://www.lorempixel.com/600/600/',
                  title: 'sample 5', image: 'http://www.lorempixel.com/400/310/'
                  ];

                  let result = data.map(Object.values);

                  console.log(result);

                  .as-console-wrapper max-height: 100% !important; top: 0; 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 6:37









                  Mohammad Usman

                  19.2k103653




                  19.2k103653






















                      up vote
                      1
                      down vote













                      All you need is a simple .map - extract the appropriate property values from the object, and return an array containing those two values:






                      var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                      console.log(
                      items.map(( title, image ) => [title, image])
                      );








                      share|improve this answer
























                        up vote
                        1
                        down vote













                        All you need is a simple .map - extract the appropriate property values from the object, and return an array containing those two values:






                        var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                        console.log(
                        items.map(( title, image ) => [title, image])
                        );








                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          All you need is a simple .map - extract the appropriate property values from the object, and return an array containing those two values:






                          var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                          console.log(
                          items.map(( title, image ) => [title, image])
                          );








                          share|improve this answer












                          All you need is a simple .map - extract the appropriate property values from the object, and return an array containing those two values:






                          var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                          console.log(
                          items.map(( title, image ) => [title, image])
                          );








                          var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                          console.log(
                          items.map(( title, image ) => [title, image])
                          );





                          var items=[title:'sample 1',image:'http://www.lorempixel.com/700/600/',title:'sample 2',image:'http://www.lorempixel.com/900/1200/',title:'sample 3',image:'http://www.lorempixel.com/400/300/',title:'sample 4',image:'http://www.lorempixel.com/600/600/',title:'sample 5',image:'http://www.lorempixel.com/400/310/',title:'sample 6',image:'http://www.lorempixel.com/410/300/',title:'sample 7',image:'http://www.lorempixel.com/500/300/',title:'sample 8',image:'http://www.lorempixel.com/300/300/',title:'sample 9',image:'http://www.lorempixel.com/450/320/',title:'sample 10',image:'http://www.lorempixel.com/500/400/'];

                          console.log(
                          items.map(( title, image ) => [title, image])
                          );






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 10 at 6:37









                          CertainPerformance

                          69.4k143453




                          69.4k143453



























                              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%2f53236592%2fconvert-array-of-objects-to-array-of-arrays%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

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo