Extract data stored in string
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
add a comment |
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
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
add a comment |
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
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
javascript
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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;
}
add a comment |
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);add a comment |
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));add a comment |
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>add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 22 '18 at 10:13
EaswarEaswar
7598
7598
add a comment |
add a comment |
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);add a comment |
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);add a comment |
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);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);answered Nov 22 '18 at 10:18
AhmadAhmad
8,21543563
8,21543563
add a comment |
add a comment |
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));add a comment |
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));add a comment |
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));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));answered Nov 22 '18 at 10:28
Alex GAlex G
1,221139
1,221139
add a comment |
add a comment |
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>add a comment |
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>add a comment |
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>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>edited Nov 22 '18 at 10:39
answered Nov 22 '18 at 10:21
ShashidharaShashidhara
450311
450311
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 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