To format javascript Object with Array of Objects to Array Of Arrays
I have the below object which has array of objects. I want format this object to object of Array of arrays
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.
Expected output is
var object1={
"Usa":[
["123245235",1],
["21423435",2]
],
"india":[
["234325436",2],
["23422464",3]
]
}
javascript arrays json object
add a comment |
I have the below object which has array of objects. I want format this object to object of Array of arrays
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.
Expected output is
var object1={
"Usa":[
["123245235",1],
["21423435",2]
],
"india":[
["234325436",2],
["23422464",3]
]
}
javascript arrays json object
add a comment |
I have the below object which has array of objects. I want format this object to object of Array of arrays
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.
Expected output is
var object1={
"Usa":[
["123245235",1],
["21423435",2]
],
"india":[
["234325436",2],
["23422464",3]
]
}
javascript arrays json object
I have the below object which has array of objects. I want format this object to object of Array of arrays
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
But the output is, in every array there are 4 arrays instead of 2 respective arrays. Which means in every array, i am getting total arrays of size 4.
Expected output is
var object1={
"Usa":[
["123245235",1],
["21423435",2]
],
"india":[
["234325436",2],
["23422464",3]
]
}
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
var object1 = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [
{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
export const createDataPoint = (period, qty) => {
return [
Date.now(time),
Math.round((Math.random() * 100) * 2) / 2 + quantity,
];
};
export function createData(object1) {
let data = ;
let total = ;
Object.keys(object1).map(function(item, index) {
object1[item].map(function(item2, index2) {
data.push(createDataPoint(item2.period, item2.qty));
})
object1[item] = data
})
// console.log(object1)
return object1;
}
javascript arrays json object
javascript arrays json object
edited Nov 13 '18 at 0:51
Barmar
423k35244346
423k35244346
asked Nov 13 '18 at 0:34
user8599269user8599269
967
967
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
add a comment |
This is an alternative using the function Object.entries
to get the entries as follow [country, array]
and then with the function reduce
build the desired output.
The function map
will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys
& reduce
:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
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%2f53272103%2fto-format-javascript-object-with-array-of-objects-to-array-of-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
add a comment |
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
add a comment |
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
Just reassign each array to new mapped array
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
const createDataPoint = ({period, qty})=>{
// do your processing here, just returning period & qty for simplicity
return [period, qty]
}
Object.keys(data).forEach(k=>{
data[k] = data[k].map(createDataPoint);
})
console.log(data)
<script>
const data = {
"Usa": [{
"period": "2018-11-03T00:00:00.000+0000",
"qty": 1
}, {
"period": "2018-11-04T00:00:00.000+0000",
"qty": 2
}],
"india": [{
"period": "2018-19-03T00:00:00.000+0000",
"qty": 2
}, {
"period": "2018-19-04T00:00:00.000+0000",
"qty": 3
}
]
}
</script>
edited Nov 13 '18 at 1:00
answered Nov 13 '18 at 0:42
charlietflcharlietfl
139k1387120
139k1387120
add a comment |
add a comment |
This is an alternative using the function Object.entries
to get the entries as follow [country, array]
and then with the function reduce
build the desired output.
The function map
will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
This is an alternative using the function Object.entries
to get the entries as follow [country, array]
and then with the function reduce
build the desired output.
The function map
will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
This is an alternative using the function Object.entries
to get the entries as follow [country, array]
and then with the function reduce
build the desired output.
The function map
will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is an alternative using the function Object.entries
to get the entries as follow [country, array]
and then with the function reduce
build the desired output.
The function map
will generate the array with tow indexes.
This approach doesn't mutate the original object
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var object1={ "Usa":[ {"period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 } ], "india":[ { "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 } ] },
result = Object.entries(object1).reduce((a, [country, arr]) => {
return Object.assign(a, {[country]: arr.map(({period, qty}) => [Date.now(period), qty])});
}, Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 13 '18 at 0:50
EleEle
22.8k42044
22.8k42044
add a comment |
add a comment |
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys
& reduce
:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
add a comment |
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys
& reduce
:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
add a comment |
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys
& reduce
:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
You can use Array.prototype.reduce & Object.entries:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
Somewhat simple approach can be done via Object.keys
& reduce
:
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.entries(obj)
.reduce((r, [k,v]) => (r[k] = v.map(({period, qty}) => [period, qty]), r),{})
console.log(result)
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
var obj = { "Usa": [{ "period": "2018-11-03T00:00:00.000+0000", "qty": 1 }, { "period": "2018-11-04T00:00:00.000+0000", "qty": 2 }], "india": [{ "period": "2018-19-03T00:00:00.000+0000", "qty": 2 }, { "period": "2018-19-04T00:00:00.000+0000", "qty": 3 }] }
const result = Object.keys(obj)
.reduce((r, c) => (r[c] = obj[c].map(({period, qty}) => [period, qty]), r),{})
console.log(result)
edited Nov 13 '18 at 3:32
answered Nov 13 '18 at 3:14
AkrionAkrion
9,40211224
9,40211224
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%2f53272103%2fto-format-javascript-object-with-array-of-objects-to-array-of-arrays%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