JavaScript - make object from multiple arrays









up vote
1
down vote

favorite












I have 3 arrays:



colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];


And want to achieve something like this:



results: '0', 'positionToId: '31', colors: 'black',
results: '1', 'positionToId: '2', colors: 'red',
results: '2', 'positionToId: '11', colors: 'black'


How can I do that?
Thanks.










share|improve this question



















  • 3




    Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 11 at 1:52










  • moved image to code in the question
    – lucascaro
    Nov 11 at 22:46














up vote
1
down vote

favorite












I have 3 arrays:



colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];


And want to achieve something like this:



results: '0', 'positionToId: '31', colors: 'black',
results: '1', 'positionToId: '2', colors: 'red',
results: '2', 'positionToId: '11', colors: 'black'


How can I do that?
Thanks.










share|improve this question



















  • 3




    Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 11 at 1:52










  • moved image to code in the question
    – lucascaro
    Nov 11 at 22:46












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have 3 arrays:



colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];


And want to achieve something like this:



results: '0', 'positionToId: '31', colors: 'black',
results: '1', 'positionToId: '2', colors: 'red',
results: '2', 'positionToId: '11', colors: 'black'


How can I do that?
Thanks.










share|improve this question















I have 3 arrays:



colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];


And want to achieve something like this:



results: '0', 'positionToId: '31', colors: 'black',
results: '1', 'positionToId: '2', colors: 'red',
results: '2', 'positionToId: '11', colors: 'black'


How can I do that?
Thanks.







javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:46









lucascaro

3,32611530




3,32611530










asked Nov 11 at 1:26









Medf101

394




394







  • 3




    Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 11 at 1:52










  • moved image to code in the question
    – lucascaro
    Nov 11 at 22:46












  • 3




    Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 11 at 1:52










  • moved image to code in the question
    – lucascaro
    Nov 11 at 22:46







3




3




Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– dmcgrandle
Nov 11 at 1:52




Please do not include code as an image. Include code as code. Also, what have you done so far? StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to illustrate the specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see How to Ask and take the tour.
– dmcgrandle
Nov 11 at 1:52












moved image to code in the question
– lucascaro
Nov 11 at 22:46




moved image to code in the question
– lucascaro
Nov 11 at 22:46












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










You want to merge these arrays into an array of objects with key => value.



Easiest way is to map one of them.




The map() method creates a new array with the results of calling a provided function on every element in the calling array.




Example:






colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];


console.log(results.map((result,i) =>
return
results: results[i],
positionToId: positionToId[i],
colors: colors[i],
;
));








share|improve this answer




















  • Thanks for explanation, works like a charm!
    – Medf101
    Nov 11 at 23:18

















up vote
0
down vote













Since you have an array results that you seem to want to use as your base and you want to replace these numbers, with objects keyed by the values already there I would just reduce them.



colors = ['green', 'red', 'black'];
positionToId = [0, 32, 15];
results = ["0", "1", "2"];

const newObject = results.reduce((acc, index) => (
...acc,
[index]:
positionToId: positionToId[index],
colors: colors[index],

), )


This also has the benefit of allowing you to access your items by id without having to loop over an array checking each of the id's






share|improve this answer



























    up vote
    0
    down vote
















    const colors = ['green', 'red', 'black'];
    const positionToId = [0, 32, 15];
    const results = ["0", "1", "2"];

    const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
    results[index][name] = property;
    return results;
    , results);

    let output = Array.from(Array(3), () => ());

    addProperties(colors, 'colors', output);
    addProperties(positionToId, 'positionToId', output);
    addProperties(results, 'results', output);

    console.log(output)








    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%2f53245057%2fjavascript-make-object-from-multiple-arrays%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








      up vote
      1
      down vote



      accepted










      You want to merge these arrays into an array of objects with key => value.



      Easiest way is to map one of them.




      The map() method creates a new array with the results of calling a provided function on every element in the calling array.




      Example:






      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));








      share|improve this answer




















      • Thanks for explanation, works like a charm!
        – Medf101
        Nov 11 at 23:18














      up vote
      1
      down vote



      accepted










      You want to merge these arrays into an array of objects with key => value.



      Easiest way is to map one of them.




      The map() method creates a new array with the results of calling a provided function on every element in the calling array.




      Example:






      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));








      share|improve this answer




















      • Thanks for explanation, works like a charm!
        – Medf101
        Nov 11 at 23:18












      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      You want to merge these arrays into an array of objects with key => value.



      Easiest way is to map one of them.




      The map() method creates a new array with the results of calling a provided function on every element in the calling array.




      Example:






      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));








      share|improve this answer












      You want to merge these arrays into an array of objects with key => value.



      Easiest way is to map one of them.




      The map() method creates a new array with the results of calling a provided function on every element in the calling array.




      Example:






      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));








      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));





      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];


      console.log(results.map((result,i) =>
      return
      results: results[i],
      positionToId: positionToId[i],
      colors: colors[i],
      ;
      ));






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 11 at 22:51









      lucascaro

      3,32611530




      3,32611530











      • Thanks for explanation, works like a charm!
        – Medf101
        Nov 11 at 23:18
















      • Thanks for explanation, works like a charm!
        – Medf101
        Nov 11 at 23:18















      Thanks for explanation, works like a charm!
      – Medf101
      Nov 11 at 23:18




      Thanks for explanation, works like a charm!
      – Medf101
      Nov 11 at 23:18












      up vote
      0
      down vote













      Since you have an array results that you seem to want to use as your base and you want to replace these numbers, with objects keyed by the values already there I would just reduce them.



      colors = ['green', 'red', 'black'];
      positionToId = [0, 32, 15];
      results = ["0", "1", "2"];

      const newObject = results.reduce((acc, index) => (
      ...acc,
      [index]:
      positionToId: positionToId[index],
      colors: colors[index],

      ), )


      This also has the benefit of allowing you to access your items by id without having to loop over an array checking each of the id's






      share|improve this answer
























        up vote
        0
        down vote













        Since you have an array results that you seem to want to use as your base and you want to replace these numbers, with objects keyed by the values already there I would just reduce them.



        colors = ['green', 'red', 'black'];
        positionToId = [0, 32, 15];
        results = ["0", "1", "2"];

        const newObject = results.reduce((acc, index) => (
        ...acc,
        [index]:
        positionToId: positionToId[index],
        colors: colors[index],

        ), )


        This also has the benefit of allowing you to access your items by id without having to loop over an array checking each of the id's






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Since you have an array results that you seem to want to use as your base and you want to replace these numbers, with objects keyed by the values already there I would just reduce them.



          colors = ['green', 'red', 'black'];
          positionToId = [0, 32, 15];
          results = ["0", "1", "2"];

          const newObject = results.reduce((acc, index) => (
          ...acc,
          [index]:
          positionToId: positionToId[index],
          colors: colors[index],

          ), )


          This also has the benefit of allowing you to access your items by id without having to loop over an array checking each of the id's






          share|improve this answer












          Since you have an array results that you seem to want to use as your base and you want to replace these numbers, with objects keyed by the values already there I would just reduce them.



          colors = ['green', 'red', 'black'];
          positionToId = [0, 32, 15];
          results = ["0", "1", "2"];

          const newObject = results.reduce((acc, index) => (
          ...acc,
          [index]:
          positionToId: positionToId[index],
          colors: colors[index],

          ), )


          This also has the benefit of allowing you to access your items by id without having to loop over an array checking each of the id's







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 22:57









          Carlo

          7401715




          7401715




















              up vote
              0
              down vote
















              const colors = ['green', 'red', 'black'];
              const positionToId = [0, 32, 15];
              const results = ["0", "1", "2"];

              const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
              results[index][name] = property;
              return results;
              , results);

              let output = Array.from(Array(3), () => ());

              addProperties(colors, 'colors', output);
              addProperties(positionToId, 'positionToId', output);
              addProperties(results, 'results', output);

              console.log(output)








              share|improve this answer
























                up vote
                0
                down vote
















                const colors = ['green', 'red', 'black'];
                const positionToId = [0, 32, 15];
                const results = ["0", "1", "2"];

                const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
                results[index][name] = property;
                return results;
                , results);

                let output = Array.from(Array(3), () => ());

                addProperties(colors, 'colors', output);
                addProperties(positionToId, 'positionToId', output);
                addProperties(results, 'results', output);

                console.log(output)








                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote












                  const colors = ['green', 'red', 'black'];
                  const positionToId = [0, 32, 15];
                  const results = ["0", "1", "2"];

                  const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
                  results[index][name] = property;
                  return results;
                  , results);

                  let output = Array.from(Array(3), () => ());

                  addProperties(colors, 'colors', output);
                  addProperties(positionToId, 'positionToId', output);
                  addProperties(results, 'results', output);

                  console.log(output)








                  share|improve this answer















                  const colors = ['green', 'red', 'black'];
                  const positionToId = [0, 32, 15];
                  const results = ["0", "1", "2"];

                  const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
                  results[index][name] = property;
                  return results;
                  , results);

                  let output = Array.from(Array(3), () => ());

                  addProperties(colors, 'colors', output);
                  addProperties(positionToId, 'positionToId', output);
                  addProperties(results, 'results', output);

                  console.log(output)








                  const colors = ['green', 'red', 'black'];
                  const positionToId = [0, 32, 15];
                  const results = ["0", "1", "2"];

                  const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
                  results[index][name] = property;
                  return results;
                  , results);

                  let output = Array.from(Array(3), () => ());

                  addProperties(colors, 'colors', output);
                  addProperties(positionToId, 'positionToId', output);
                  addProperties(results, 'results', output);

                  console.log(output)





                  const colors = ['green', 'red', 'black'];
                  const positionToId = [0, 32, 15];
                  const results = ["0", "1", "2"];

                  const addProperties = (properties, name, results) => properties.reduce((results, property, index) =>
                  results[index][name] = property;
                  return results;
                  , results);

                  let output = Array.from(Array(3), () => ());

                  addProperties(colors, 'colors', output);
                  addProperties(positionToId, 'positionToId', output);
                  addProperties(results, 'results', output);

                  console.log(output)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 23:02









                  Adrian Brand

                  2,45911018




                  2,45911018



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245057%2fjavascript-make-object-from-multiple-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







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands