How can I convert an Array of Key Value pairs to actual Arrayof object with values of first array as keys?











up vote
-1
down vote

favorite












My backend is returning the data as below.



   var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]


How can I convert this to an Objectof Object as below



{BOF:true,CM:true, ...}


Thanks in advance.










share|improve this question




















  • 1




    why the array in the result? what have you tried?
    – Nina Scholz
    Nov 19 at 11:48










  • In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
    – Mahesh B
    Nov 19 at 11:50










  • Yes I realized now. I need Object not the array. Corrected my question.
    – Mahesh B
    Nov 19 at 11:52










  • please close as dupe for stackoverflow.com/questions/47133075/…
    – Nina Scholz
    Nov 19 at 11:58










  • or stackoverflow.com/questions/47365354/…
    – Nina Scholz
    Nov 19 at 11:58















up vote
-1
down vote

favorite












My backend is returning the data as below.



   var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]


How can I convert this to an Objectof Object as below



{BOF:true,CM:true, ...}


Thanks in advance.










share|improve this question




















  • 1




    why the array in the result? what have you tried?
    – Nina Scholz
    Nov 19 at 11:48










  • In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
    – Mahesh B
    Nov 19 at 11:50










  • Yes I realized now. I need Object not the array. Corrected my question.
    – Mahesh B
    Nov 19 at 11:52










  • please close as dupe for stackoverflow.com/questions/47133075/…
    – Nina Scholz
    Nov 19 at 11:58










  • or stackoverflow.com/questions/47365354/…
    – Nina Scholz
    Nov 19 at 11:58













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











My backend is returning the data as below.



   var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]


How can I convert this to an Objectof Object as below



{BOF:true,CM:true, ...}


Thanks in advance.










share|improve this question















My backend is returning the data as below.



   var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]


How can I convert this to an Objectof Object as below



{BOF:true,CM:true, ...}


Thanks in advance.







javascript arrays object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 11:51

























asked Nov 19 at 11:47









Mahesh B

4019




4019








  • 1




    why the array in the result? what have you tried?
    – Nina Scholz
    Nov 19 at 11:48










  • In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
    – Mahesh B
    Nov 19 at 11:50










  • Yes I realized now. I need Object not the array. Corrected my question.
    – Mahesh B
    Nov 19 at 11:52










  • please close as dupe for stackoverflow.com/questions/47133075/…
    – Nina Scholz
    Nov 19 at 11:58










  • or stackoverflow.com/questions/47365354/…
    – Nina Scholz
    Nov 19 at 11:58














  • 1




    why the array in the result? what have you tried?
    – Nina Scholz
    Nov 19 at 11:48










  • In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
    – Mahesh B
    Nov 19 at 11:50










  • Yes I realized now. I need Object not the array. Corrected my question.
    – Mahesh B
    Nov 19 at 11:52










  • please close as dupe for stackoverflow.com/questions/47133075/…
    – Nina Scholz
    Nov 19 at 11:58










  • or stackoverflow.com/questions/47365354/…
    – Nina Scholz
    Nov 19 at 11:58








1




1




why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48




why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48












In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50




In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50












Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52




Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52












please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58




please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58












or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58




or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58












3 Answers
3






active

oldest

votes

















up vote
4
down vote













You could use some ES6 syntax, using Object.assign, spread syntax, computed properties, and then it really gets very functional programming style:






var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));

console.log(result);








share|improve this answer























  • Why would you use Object.assign ?
    – trichetriche
    Nov 19 at 11:52










  • It just is easy for converting an array of objects to one merged object.
    – trincot
    Nov 19 at 11:53










  • my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
    – trichetriche
    Nov 19 at 11:55






  • 1




    Sure, there are many ways to do it, and preferences differ.
    – trincot
    Nov 19 at 11:56










  • totally, +1 for that :)
    – trichetriche
    Nov 19 at 11:57


















up vote
1
down vote













Altough you can find the answer in the documentation of Array (disclaimer: it's map), the syntax is a bit tricky for a one-liner.



Here you go :






const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];

const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

console.log(formatted);





As for your edit, if you want to create an object, use reduce instead :






const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];

const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

console.log(formatted);








share|improve this answer




























    up vote
    0
    down vote













    Please always show your attempt in OP so that we get to know which direction you were moving..



    You can use "Array.map" and "Object.assign" like below






    var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

    let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

    console.log(result)








    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%2f53373981%2fhow-can-i-convert-an-array-of-key-value-pairs-to-actual-arrayof-object-with-valu%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
      4
      down vote













      You could use some ES6 syntax, using Object.assign, spread syntax, computed properties, and then it really gets very functional programming style:






      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);








      share|improve this answer























      • Why would you use Object.assign ?
        – trichetriche
        Nov 19 at 11:52










      • It just is easy for converting an array of objects to one merged object.
        – trincot
        Nov 19 at 11:53










      • my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
        – trichetriche
        Nov 19 at 11:55






      • 1




        Sure, there are many ways to do it, and preferences differ.
        – trincot
        Nov 19 at 11:56










      • totally, +1 for that :)
        – trichetriche
        Nov 19 at 11:57















      up vote
      4
      down vote













      You could use some ES6 syntax, using Object.assign, spread syntax, computed properties, and then it really gets very functional programming style:






      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);








      share|improve this answer























      • Why would you use Object.assign ?
        – trichetriche
        Nov 19 at 11:52










      • It just is easy for converting an array of objects to one merged object.
        – trincot
        Nov 19 at 11:53










      • my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
        – trichetriche
        Nov 19 at 11:55






      • 1




        Sure, there are many ways to do it, and preferences differ.
        – trincot
        Nov 19 at 11:56










      • totally, +1 for that :)
        – trichetriche
        Nov 19 at 11:57













      up vote
      4
      down vote










      up vote
      4
      down vote









      You could use some ES6 syntax, using Object.assign, spread syntax, computed properties, and then it really gets very functional programming style:






      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);








      share|improve this answer














      You could use some ES6 syntax, using Object.assign, spread syntax, computed properties, and then it really gets very functional programming style:






      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);








      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);





      var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];

      const result = Object.assign(...permissions.map(
      ({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
      ));

      console.log(result);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 19 at 13:20

























      answered Nov 19 at 11:49









      trincot

      113k1477109




      113k1477109












      • Why would you use Object.assign ?
        – trichetriche
        Nov 19 at 11:52










      • It just is easy for converting an array of objects to one merged object.
        – trincot
        Nov 19 at 11:53










      • my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
        – trichetriche
        Nov 19 at 11:55






      • 1




        Sure, there are many ways to do it, and preferences differ.
        – trincot
        Nov 19 at 11:56










      • totally, +1 for that :)
        – trichetriche
        Nov 19 at 11:57


















      • Why would you use Object.assign ?
        – trichetriche
        Nov 19 at 11:52










      • It just is easy for converting an array of objects to one merged object.
        – trincot
        Nov 19 at 11:53










      • my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
        – trichetriche
        Nov 19 at 11:55






      • 1




        Sure, there are many ways to do it, and preferences differ.
        – trincot
        Nov 19 at 11:56










      • totally, +1 for that :)
        – trichetriche
        Nov 19 at 11:57
















      Why would you use Object.assign ?
      – trichetriche
      Nov 19 at 11:52




      Why would you use Object.assign ?
      – trichetriche
      Nov 19 at 11:52












      It just is easy for converting an array of objects to one merged object.
      – trincot
      Nov 19 at 11:53




      It just is easy for converting an array of objects to one merged object.
      – trincot
      Nov 19 at 11:53












      my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
      – trichetriche
      Nov 19 at 11:55




      my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer reduce ...
      – trichetriche
      Nov 19 at 11:55




      1




      1




      Sure, there are many ways to do it, and preferences differ.
      – trincot
      Nov 19 at 11:56




      Sure, there are many ways to do it, and preferences differ.
      – trincot
      Nov 19 at 11:56












      totally, +1 for that :)
      – trichetriche
      Nov 19 at 11:57




      totally, +1 for that :)
      – trichetriche
      Nov 19 at 11:57












      up vote
      1
      down vote













      Altough you can find the answer in the documentation of Array (disclaimer: it's map), the syntax is a bit tricky for a one-liner.



      Here you go :






      const data = [
      {"PermCode":"BOF","IsvalidPerm":true},
      {"PermCode":"CM","IsvalidPerm":false},
      {"PermCode":"CV","IsvalidPerm":true},
      {"PermCode":"DAS","IsvalidPerm":true},
      {"PermCode":"RPT","IsvalidPerm":true},
      {"PermCode":"VM","IsvalidPerm":true}
      ];

      const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

      console.log(formatted);





      As for your edit, if you want to create an object, use reduce instead :






      const data = [
      {"PermCode":"BOF","IsvalidPerm":true},
      {"PermCode":"CM","IsvalidPerm":false},
      {"PermCode":"CV","IsvalidPerm":true},
      {"PermCode":"DAS","IsvalidPerm":true},
      {"PermCode":"RPT","IsvalidPerm":true},
      {"PermCode":"VM","IsvalidPerm":true}
      ];

      const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

      console.log(formatted);








      share|improve this answer

























        up vote
        1
        down vote













        Altough you can find the answer in the documentation of Array (disclaimer: it's map), the syntax is a bit tricky for a one-liner.



        Here you go :






        const data = [
        {"PermCode":"BOF","IsvalidPerm":true},
        {"PermCode":"CM","IsvalidPerm":false},
        {"PermCode":"CV","IsvalidPerm":true},
        {"PermCode":"DAS","IsvalidPerm":true},
        {"PermCode":"RPT","IsvalidPerm":true},
        {"PermCode":"VM","IsvalidPerm":true}
        ];

        const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

        console.log(formatted);





        As for your edit, if you want to create an object, use reduce instead :






        const data = [
        {"PermCode":"BOF","IsvalidPerm":true},
        {"PermCode":"CM","IsvalidPerm":false},
        {"PermCode":"CV","IsvalidPerm":true},
        {"PermCode":"DAS","IsvalidPerm":true},
        {"PermCode":"RPT","IsvalidPerm":true},
        {"PermCode":"VM","IsvalidPerm":true}
        ];

        const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

        console.log(formatted);








        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          Altough you can find the answer in the documentation of Array (disclaimer: it's map), the syntax is a bit tricky for a one-liner.



          Here you go :






          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

          console.log(formatted);





          As for your edit, if you want to create an object, use reduce instead :






          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

          console.log(formatted);








          share|improve this answer












          Altough you can find the answer in the documentation of Array (disclaimer: it's map), the syntax is a bit tricky for a one-liner.



          Here you go :






          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

          console.log(formatted);





          As for your edit, if you want to create an object, use reduce instead :






          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

          console.log(formatted);








          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

          console.log(formatted);





          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));

          console.log(formatted);





          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

          console.log(formatted);





          const data = [
          {"PermCode":"BOF","IsvalidPerm":true},
          {"PermCode":"CM","IsvalidPerm":false},
          {"PermCode":"CV","IsvalidPerm":true},
          {"PermCode":"DAS","IsvalidPerm":true},
          {"PermCode":"RPT","IsvalidPerm":true},
          {"PermCode":"VM","IsvalidPerm":true}
          ];

          const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});

          console.log(formatted);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 11:50









          trichetriche

          23.7k41949




          23.7k41949






















              up vote
              0
              down vote













              Please always show your attempt in OP so that we get to know which direction you were moving..



              You can use "Array.map" and "Object.assign" like below






              var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

              let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

              console.log(result)








              share|improve this answer

























                up vote
                0
                down vote













                Please always show your attempt in OP so that we get to know which direction you were moving..



                You can use "Array.map" and "Object.assign" like below






                var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

                let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

                console.log(result)








                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Please always show your attempt in OP so that we get to know which direction you were moving..



                  You can use "Array.map" and "Object.assign" like below






                  var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

                  let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

                  console.log(result)








                  share|improve this answer












                  Please always show your attempt in OP so that we get to know which direction you were moving..



                  You can use "Array.map" and "Object.assign" like below






                  var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

                  let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

                  console.log(result)








                  var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

                  let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

                  console.log(result)





                  var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]

                  let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))

                  console.log(result)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 11:51









                  Nitish Narang

                  2,061611




                  2,061611






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373981%2fhow-can-i-convert-an-array-of-key-value-pairs-to-actual-arrayof-object-with-valu%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

                      Wiesbaden

                      Marschland

                      Dieringhausen