To format javascript Object with Array of Objects to Array Of Arrays












0















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]
]
}









share|improve this question





























    0















    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]
    ]
    }









    share|improve this question



























      0












      0








      0








      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]
      ]
      }









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:51









      Barmar

      423k35244346




      423k35244346










      asked Nov 13 '18 at 0:34









      user8599269user8599269

      967




      967
























          3 Answers
          3






          active

          oldest

          votes


















          1














          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>








          share|improve this answer

































            1














            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; }








            share|improve this answer































              1














              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)








              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%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









                1














                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>








                share|improve this answer






























                  1














                  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>








                  share|improve this answer




























                    1












                    1








                    1







                    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>








                    share|improve this answer















                    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>






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 1:00

























                    answered Nov 13 '18 at 0:42









                    charlietflcharlietfl

                    139k1387120




                    139k1387120

























                        1














                        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; }








                        share|improve this answer




























                          1














                          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; }








                          share|improve this answer


























                            1












                            1








                            1







                            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; }








                            share|improve this answer













                            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; }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 0:50









                            EleEle

                            22.8k42044




                            22.8k42044























                                1














                                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)








                                share|improve this answer






























                                  1














                                  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)








                                  share|improve this answer




























                                    1












                                    1








                                    1







                                    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)








                                    share|improve this answer















                                    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)






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 13 '18 at 3:32

























                                    answered Nov 13 '18 at 3:14









                                    AkrionAkrion

                                    9,40211224




                                    9,40211224






























                                        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%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





















































                                        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

                                        Full-time equivalent

                                        Bicuculline

                                        さくらももこ