Extract data stored in string












0















Is there a way I can create an object from this data stored in a string, coming from the google API:



   "recurrence": [
"RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"
],


I can use string methods, but I wondered if there was a better way.



Something like:



recurrence: {
frequency: "weekly",
until: "yy/mm/dd",
byday: "th"
}


Any ideas?










share|improve this question

























  • What is the desired object structure, based on the given string?

    – Ahmad
    Nov 22 '18 at 10:06













  • You could split on semicolon, which would give you an array of entries.

    – Tim Biegeleisen
    Nov 22 '18 at 10:06











  • No way but string methods for this case.

    – hindmost
    Nov 22 '18 at 10:11
















0















Is there a way I can create an object from this data stored in a string, coming from the google API:



   "recurrence": [
"RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"
],


I can use string methods, but I wondered if there was a better way.



Something like:



recurrence: {
frequency: "weekly",
until: "yy/mm/dd",
byday: "th"
}


Any ideas?










share|improve this question

























  • What is the desired object structure, based on the given string?

    – Ahmad
    Nov 22 '18 at 10:06













  • You could split on semicolon, which would give you an array of entries.

    – Tim Biegeleisen
    Nov 22 '18 at 10:06











  • No way but string methods for this case.

    – hindmost
    Nov 22 '18 at 10:11














0












0








0








Is there a way I can create an object from this data stored in a string, coming from the google API:



   "recurrence": [
"RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"
],


I can use string methods, but I wondered if there was a better way.



Something like:



recurrence: {
frequency: "weekly",
until: "yy/mm/dd",
byday: "th"
}


Any ideas?










share|improve this question
















Is there a way I can create an object from this data stored in a string, coming from the google API:



   "recurrence": [
"RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"
],


I can use string methods, but I wondered if there was a better way.



Something like:



recurrence: {
frequency: "weekly",
until: "yy/mm/dd",
byday: "th"
}


Any ideas?







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:08







Bomber

















asked Nov 22 '18 at 10:05









BomberBomber

2,028113568




2,028113568













  • What is the desired object structure, based on the given string?

    – Ahmad
    Nov 22 '18 at 10:06













  • You could split on semicolon, which would give you an array of entries.

    – Tim Biegeleisen
    Nov 22 '18 at 10:06











  • No way but string methods for this case.

    – hindmost
    Nov 22 '18 at 10:11



















  • What is the desired object structure, based on the given string?

    – Ahmad
    Nov 22 '18 at 10:06













  • You could split on semicolon, which would give you an array of entries.

    – Tim Biegeleisen
    Nov 22 '18 at 10:06











  • No way but string methods for this case.

    – hindmost
    Nov 22 '18 at 10:11

















What is the desired object structure, based on the given string?

– Ahmad
Nov 22 '18 at 10:06







What is the desired object structure, based on the given string?

– Ahmad
Nov 22 '18 at 10:06















You could split on semicolon, which would give you an array of entries.

– Tim Biegeleisen
Nov 22 '18 at 10:06





You could split on semicolon, which would give you an array of entries.

– Tim Biegeleisen
Nov 22 '18 at 10:06













No way but string methods for this case.

– hindmost
Nov 22 '18 at 10:11





No way but string methods for this case.

– hindmost
Nov 22 '18 at 10:11












4 Answers
4






active

oldest

votes


















0














The Google API Documentation itself provides no other format. Hence your only option would be to apply simple string transformations. Such as :



let targetObj = {};
for(const entry of res.recurrence.substr(5).split(';)) {
const [key, value] = entry.split('=');
targetObj[key] = value;
}





share|improve this answer































    0














    You will need to split multiple times and convert key to lower case.






    var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
    var obj = {};
    var str = api.recurrence[0];
    var arr = str.split(";");
    arr.forEach(function(element){

    var pair = element.split("=");
    var key_array = pair[0].split(":");
    var key = key_array[key_array.length-1].toLowerCase();
    var val = pair[1];
    obj[key]=val;

    });

    console.log(obj);








    share|improve this answer































      0














      You can use a regex to match what you want and create your object.






      const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

      const regex = /w+=w+/g;

      const extract = (obj) =>

      obj.reference.reduce((acc, val) => {

      val.match(regex).forEach((match) => {

      const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

      acc[lVal] = rVal;
      });

      return acc;
      }, {});

      console.log(extract(obj));








      share|improve this answer































        0














        Look at my solution






        var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
        var str2 = str1.split(':')[1].split(';');
        // console.log(str2.split(';'));
        var x = str2.map((s)=>{
        return s.split("=");
        });

        var recurrence = {};
        x.forEach((ar)=>{
        if(ar[0] == 'UNTIL')
        ar[1] = moment(ar[1]).format('DD/MM/YYYY');
        recurrence[ar[0]] = ar[1];
        });

        console.log(recurrence);

        <script src="https://momentjs.com/downloads/moment.js"></script>








        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%2f53428426%2fextract-data-stored-in-string%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














          The Google API Documentation itself provides no other format. Hence your only option would be to apply simple string transformations. Such as :



          let targetObj = {};
          for(const entry of res.recurrence.substr(5).split(';)) {
          const [key, value] = entry.split('=');
          targetObj[key] = value;
          }





          share|improve this answer




























            0














            The Google API Documentation itself provides no other format. Hence your only option would be to apply simple string transformations. Such as :



            let targetObj = {};
            for(const entry of res.recurrence.substr(5).split(';)) {
            const [key, value] = entry.split('=');
            targetObj[key] = value;
            }





            share|improve this answer


























              0












              0








              0







              The Google API Documentation itself provides no other format. Hence your only option would be to apply simple string transformations. Such as :



              let targetObj = {};
              for(const entry of res.recurrence.substr(5).split(';)) {
              const [key, value] = entry.split('=');
              targetObj[key] = value;
              }





              share|improve this answer













              The Google API Documentation itself provides no other format. Hence your only option would be to apply simple string transformations. Such as :



              let targetObj = {};
              for(const entry of res.recurrence.substr(5).split(';)) {
              const [key, value] = entry.split('=');
              targetObj[key] = value;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 10:13









              EaswarEaswar

              7598




              7598

























                  0














                  You will need to split multiple times and convert key to lower case.






                  var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                  var obj = {};
                  var str = api.recurrence[0];
                  var arr = str.split(";");
                  arr.forEach(function(element){

                  var pair = element.split("=");
                  var key_array = pair[0].split(":");
                  var key = key_array[key_array.length-1].toLowerCase();
                  var val = pair[1];
                  obj[key]=val;

                  });

                  console.log(obj);








                  share|improve this answer




























                    0














                    You will need to split multiple times and convert key to lower case.






                    var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                    var obj = {};
                    var str = api.recurrence[0];
                    var arr = str.split(";");
                    arr.forEach(function(element){

                    var pair = element.split("=");
                    var key_array = pair[0].split(":");
                    var key = key_array[key_array.length-1].toLowerCase();
                    var val = pair[1];
                    obj[key]=val;

                    });

                    console.log(obj);








                    share|improve this answer


























                      0












                      0








                      0







                      You will need to split multiple times and convert key to lower case.






                      var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                      var obj = {};
                      var str = api.recurrence[0];
                      var arr = str.split(";");
                      arr.forEach(function(element){

                      var pair = element.split("=");
                      var key_array = pair[0].split(":");
                      var key = key_array[key_array.length-1].toLowerCase();
                      var val = pair[1];
                      obj[key]=val;

                      });

                      console.log(obj);








                      share|improve this answer













                      You will need to split multiple times and convert key to lower case.






                      var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                      var obj = {};
                      var str = api.recurrence[0];
                      var arr = str.split(";");
                      arr.forEach(function(element){

                      var pair = element.split("=");
                      var key_array = pair[0].split(":");
                      var key = key_array[key_array.length-1].toLowerCase();
                      var val = pair[1];
                      obj[key]=val;

                      });

                      console.log(obj);








                      var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                      var obj = {};
                      var str = api.recurrence[0];
                      var arr = str.split(";");
                      arr.forEach(function(element){

                      var pair = element.split("=");
                      var key_array = pair[0].split(":");
                      var key = key_array[key_array.length-1].toLowerCase();
                      var val = pair[1];
                      obj[key]=val;

                      });

                      console.log(obj);





                      var api = {"recurrence": ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"]};
                      var obj = {};
                      var str = api.recurrence[0];
                      var arr = str.split(";");
                      arr.forEach(function(element){

                      var pair = element.split("=");
                      var key_array = pair[0].split(":");
                      var key = key_array[key_array.length-1].toLowerCase();
                      var val = pair[1];
                      obj[key]=val;

                      });

                      console.log(obj);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 10:18









                      AhmadAhmad

                      8,21543563




                      8,21543563























                          0














                          You can use a regex to match what you want and create your object.






                          const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                          const regex = /w+=w+/g;

                          const extract = (obj) =>

                          obj.reference.reduce((acc, val) => {

                          val.match(regex).forEach((match) => {

                          const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                          acc[lVal] = rVal;
                          });

                          return acc;
                          }, {});

                          console.log(extract(obj));








                          share|improve this answer




























                            0














                            You can use a regex to match what you want and create your object.






                            const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                            const regex = /w+=w+/g;

                            const extract = (obj) =>

                            obj.reference.reduce((acc, val) => {

                            val.match(regex).forEach((match) => {

                            const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                            acc[lVal] = rVal;
                            });

                            return acc;
                            }, {});

                            console.log(extract(obj));








                            share|improve this answer


























                              0












                              0








                              0







                              You can use a regex to match what you want and create your object.






                              const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                              const regex = /w+=w+/g;

                              const extract = (obj) =>

                              obj.reference.reduce((acc, val) => {

                              val.match(regex).forEach((match) => {

                              const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                              acc[lVal] = rVal;
                              });

                              return acc;
                              }, {});

                              console.log(extract(obj));








                              share|improve this answer













                              You can use a regex to match what you want and create your object.






                              const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                              const regex = /w+=w+/g;

                              const extract = (obj) =>

                              obj.reference.reduce((acc, val) => {

                              val.match(regex).forEach((match) => {

                              const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                              acc[lVal] = rVal;
                              });

                              return acc;
                              }, {});

                              console.log(extract(obj));








                              const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                              const regex = /w+=w+/g;

                              const extract = (obj) =>

                              obj.reference.reduce((acc, val) => {

                              val.match(regex).forEach((match) => {

                              const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                              acc[lVal] = rVal;
                              });

                              return acc;
                              }, {});

                              console.log(extract(obj));





                              const obj = { reference: ["RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH"] };

                              const regex = /w+=w+/g;

                              const extract = (obj) =>

                              obj.reference.reduce((acc, val) => {

                              val.match(regex).forEach((match) => {

                              const [lVal, rVal] = match.split('=').map(d => d.toLowerCase());

                              acc[lVal] = rVal;
                              });

                              return acc;
                              }, {});

                              console.log(extract(obj));






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 10:28









                              Alex GAlex G

                              1,221139




                              1,221139























                                  0














                                  Look at my solution






                                  var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                  var str2 = str1.split(':')[1].split(';');
                                  // console.log(str2.split(';'));
                                  var x = str2.map((s)=>{
                                  return s.split("=");
                                  });

                                  var recurrence = {};
                                  x.forEach((ar)=>{
                                  if(ar[0] == 'UNTIL')
                                  ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                  recurrence[ar[0]] = ar[1];
                                  });

                                  console.log(recurrence);

                                  <script src="https://momentjs.com/downloads/moment.js"></script>








                                  share|improve this answer






























                                    0














                                    Look at my solution






                                    var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                    var str2 = str1.split(':')[1].split(';');
                                    // console.log(str2.split(';'));
                                    var x = str2.map((s)=>{
                                    return s.split("=");
                                    });

                                    var recurrence = {};
                                    x.forEach((ar)=>{
                                    if(ar[0] == 'UNTIL')
                                    ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                    recurrence[ar[0]] = ar[1];
                                    });

                                    console.log(recurrence);

                                    <script src="https://momentjs.com/downloads/moment.js"></script>








                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Look at my solution






                                      var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                      var str2 = str1.split(':')[1].split(';');
                                      // console.log(str2.split(';'));
                                      var x = str2.map((s)=>{
                                      return s.split("=");
                                      });

                                      var recurrence = {};
                                      x.forEach((ar)=>{
                                      if(ar[0] == 'UNTIL')
                                      ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                      recurrence[ar[0]] = ar[1];
                                      });

                                      console.log(recurrence);

                                      <script src="https://momentjs.com/downloads/moment.js"></script>








                                      share|improve this answer















                                      Look at my solution






                                      var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                      var str2 = str1.split(':')[1].split(';');
                                      // console.log(str2.split(';'));
                                      var x = str2.map((s)=>{
                                      return s.split("=");
                                      });

                                      var recurrence = {};
                                      x.forEach((ar)=>{
                                      if(ar[0] == 'UNTIL')
                                      ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                      recurrence[ar[0]] = ar[1];
                                      });

                                      console.log(recurrence);

                                      <script src="https://momentjs.com/downloads/moment.js"></script>








                                      var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                      var str2 = str1.split(':')[1].split(';');
                                      // console.log(str2.split(';'));
                                      var x = str2.map((s)=>{
                                      return s.split("=");
                                      });

                                      var recurrence = {};
                                      x.forEach((ar)=>{
                                      if(ar[0] == 'UNTIL')
                                      ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                      recurrence[ar[0]] = ar[1];
                                      });

                                      console.log(recurrence);

                                      <script src="https://momentjs.com/downloads/moment.js"></script>





                                      var str1 = "RRULE:FREQ=WEEKLY;UNTIL=20181213T235959Z;BYDAY=TH";
                                      var str2 = str1.split(':')[1].split(';');
                                      // console.log(str2.split(';'));
                                      var x = str2.map((s)=>{
                                      return s.split("=");
                                      });

                                      var recurrence = {};
                                      x.forEach((ar)=>{
                                      if(ar[0] == 'UNTIL')
                                      ar[1] = moment(ar[1]).format('DD/MM/YYYY');
                                      recurrence[ar[0]] = ar[1];
                                      });

                                      console.log(recurrence);

                                      <script src="https://momentjs.com/downloads/moment.js"></script>






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 22 '18 at 10:39

























                                      answered Nov 22 '18 at 10:21









                                      ShashidharaShashidhara

                                      450311




                                      450311






























                                          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%2f53428426%2fextract-data-stored-in-string%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

                                          Ottavio Pratesi

                                          Tricia Helfer

                                          15 giugno