Create JSON string from a Map with arrays as entries
My data structure object is a Map with the following structure:
"2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
I use JSON.stringify
and then JSON.parse
. However, I would need the elements to be somehow named so that I can reference them (e.g. when drawing a chart).
I tried the following but I get NaN
as a value.
data = Object.keys(data).map(function (k)
return date: new Date(k), value: +data[k];
);
I would like to have something like this:
Key: "2018-09-25"
1:0
2:1
3:2
4:0
5:8
javascript collections
|
show 3 more comments
My data structure object is a Map with the following structure:
"2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
I use JSON.stringify
and then JSON.parse
. However, I would need the elements to be somehow named so that I can reference them (e.g. when drawing a chart).
I tried the following but I get NaN
as a value.
data = Object.keys(data).map(function (k)
return date: new Date(k), value: +data[k];
);
I would like to have something like this:
Key: "2018-09-25"
1:0
2:1
3:2
4:0
5:8
javascript collections
3
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you callJSON.stringify()
and thenJSON.parse()
on the data.
– Pointy
Nov 15 '18 at 17:24
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
2
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36
|
show 3 more comments
My data structure object is a Map with the following structure:
"2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
I use JSON.stringify
and then JSON.parse
. However, I would need the elements to be somehow named so that I can reference them (e.g. when drawing a chart).
I tried the following but I get NaN
as a value.
data = Object.keys(data).map(function (k)
return date: new Date(k), value: +data[k];
);
I would like to have something like this:
Key: "2018-09-25"
1:0
2:1
3:2
4:0
5:8
javascript collections
My data structure object is a Map with the following structure:
"2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
I use JSON.stringify
and then JSON.parse
. However, I would need the elements to be somehow named so that I can reference them (e.g. when drawing a chart).
I tried the following but I get NaN
as a value.
data = Object.keys(data).map(function (k)
return date: new Date(k), value: +data[k];
);
I would like to have something like this:
Key: "2018-09-25"
1:0
2:1
3:2
4:0
5:8
javascript collections
javascript collections
edited Nov 15 '18 at 17:35
ponury-kostek
4,77841224
4,77841224
asked Nov 15 '18 at 17:23
John VJohn V
2,02482546
2,02482546
3
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you callJSON.stringify()
and thenJSON.parse()
on the data.
– Pointy
Nov 15 '18 at 17:24
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
2
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36
|
show 3 more comments
3
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you callJSON.stringify()
and thenJSON.parse()
on the data.
– Pointy
Nov 15 '18 at 17:24
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
2
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36
3
3
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you call
JSON.stringify()
and then JSON.parse()
on the data.– Pointy
Nov 15 '18 at 17:24
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you call
JSON.stringify()
and then JSON.parse()
on the data.– Pointy
Nov 15 '18 at 17:24
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
2
2
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36
|
show 3 more comments
1 Answer
1
active
oldest
votes
Since you are using map()
it looks like you want an array as a final result. But, since the array will be a different size, map()
doesn't really work. You can use reduce to flatten out the inner arrays into one big array:
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
Is that what you're after?
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is justnewAr.length
, is that what you mean?
– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
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%2f53324850%2fcreate-json-string-from-a-map-with-arrays-as-entries%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you are using map()
it looks like you want an array as a final result. But, since the array will be a different size, map()
doesn't really work. You can use reduce to flatten out the inner arrays into one big array:
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
Is that what you're after?
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is justnewAr.length
, is that what you mean?
– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
add a comment |
Since you are using map()
it looks like you want an array as a final result. But, since the array will be a different size, map()
doesn't really work. You can use reduce to flatten out the inner arrays into one big array:
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
Is that what you're after?
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is justnewAr.length
, is that what you mean?
– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
add a comment |
Since you are using map()
it looks like you want an array as a final result. But, since the array will be a different size, map()
doesn't really work. You can use reduce to flatten out the inner arrays into one big array:
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
Is that what you're after?
Since you are using map()
it looks like you want an array as a final result. But, since the array will be a different size, map()
doesn't really work. You can use reduce to flatten out the inner arrays into one big array:
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
Is that what you're after?
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
let o = "2018-09-25":[0,1,2,0,8],
"2018-10-17":[0,2,0,0,0],
"2018-10-26":[0,2,1,0,0],
"2018-10-29":[0,2,2,1,0],
"2018-10-31":[0,3,2,1,0],
"2018-11-01":[0,3,3,1,0],
"2018-11-02":[0,4,4,1,0]
let newAr = Object.entries(o).reduce((arr, [key, values]) =>
return arr.concat(values.map(n => (date: new Date(key), value: n)))
, )
console.log(newAr)
answered Nov 15 '18 at 17:40
Mark MeyerMark Meyer
39.5k33162
39.5k33162
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is justnewAr.length
, is that what you mean?
– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
add a comment |
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is justnewAr.length
, is that what you mean?
– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
1
1
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
Thanks, yes! It provides the format I was trying to get.
– John V
Nov 15 '18 at 17:41
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
May I ask, how do I get the length of the array? I tried something like newAr.Value.length but that does not work.
– John V
Nov 15 '18 at 17:57
The length of the final array is just
newAr.length
, is that what you mean?– Mark Meyer
Nov 15 '18 at 18:00
The length of the final array is just
newAr.length
, is that what you mean?– Mark Meyer
Nov 15 '18 at 18:00
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
No, I mean I should get 5 - there are 5 elements in an array under each key.
– John V
Nov 15 '18 at 18:02
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
Now that it's flattened out, it's a little work to get the length. The easiest way is to just look at your original object and get the length of the array. In the example above: ` o['2018-09-25'].length`. Otherwise you'll need to filter or reduce the result to regroup the items.
– Mark Meyer
Nov 15 '18 at 18:04
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%2f53324850%2fcreate-json-string-from-a-map-with-arrays-as-entries%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
3
It would help if you'd post the actual data structure in JavaScript terms, or at least a representative part of it. Also, it would be good to explain why you call
JSON.stringify()
and thenJSON.parse()
on the data.– Pointy
Nov 15 '18 at 17:24
Please also add the data structure you would like to transform your map to.
– Fabian Hinsenkamp
Nov 15 '18 at 17:29
Thanks for adding the desired result, but it's not clear what you want. Why not just post actual javascript?
– Mark Meyer
Nov 15 '18 at 17:34
2
Remove the "+" at "return date: new Date(k), value: +data[k];". The correct line is "return date: new Date(k), value: data[k];"
– TwilightTitus
Nov 15 '18 at 17:35
@MarkMeyer Basically I want to draw a chart. But I do not know how to reference individual elements in the arrays. The chart should have 5 lines, for each element.
– John V
Nov 15 '18 at 17:36