how to push all objects keys to a separate array in array using angular or javascript?












0















I have array of objects like this



let array = [
{'name' : 'Jack', 'age':23},
{'name': 'Robin', 'age':25}
];


and I want an array like this



[
['Jack',23],
['Robin',25]
]


I tried this code



var myArr = ; 

var input = [
{name : 'Jack', age : 23},
{name : 'Robin', age : 25}
];

input.forEach((item,index)=>{
for (var k in item) {
myArr.push(item[k]);
}
})


But it is producing result like this



["Jack", 23, "Robin", 25]









share|improve this question





























    0















    I have array of objects like this



    let array = [
    {'name' : 'Jack', 'age':23},
    {'name': 'Robin', 'age':25}
    ];


    and I want an array like this



    [
    ['Jack',23],
    ['Robin',25]
    ]


    I tried this code



    var myArr = ; 

    var input = [
    {name : 'Jack', age : 23},
    {name : 'Robin', age : 25}
    ];

    input.forEach((item,index)=>{
    for (var k in item) {
    myArr.push(item[k]);
    }
    })


    But it is producing result like this



    ["Jack", 23, "Robin", 25]









    share|improve this question



























      0












      0








      0








      I have array of objects like this



      let array = [
      {'name' : 'Jack', 'age':23},
      {'name': 'Robin', 'age':25}
      ];


      and I want an array like this



      [
      ['Jack',23],
      ['Robin',25]
      ]


      I tried this code



      var myArr = ; 

      var input = [
      {name : 'Jack', age : 23},
      {name : 'Robin', age : 25}
      ];

      input.forEach((item,index)=>{
      for (var k in item) {
      myArr.push(item[k]);
      }
      })


      But it is producing result like this



      ["Jack", 23, "Robin", 25]









      share|improve this question
















      I have array of objects like this



      let array = [
      {'name' : 'Jack', 'age':23},
      {'name': 'Robin', 'age':25}
      ];


      and I want an array like this



      [
      ['Jack',23],
      ['Robin',25]
      ]


      I tried this code



      var myArr = ; 

      var input = [
      {name : 'Jack', age : 23},
      {name : 'Robin', age : 25}
      ];

      input.forEach((item,index)=>{
      for (var k in item) {
      myArr.push(item[k]);
      }
      })


      But it is producing result like this



      ["Jack", 23, "Robin", 25]






      javascript arrays object angular5 javascript-objects






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 7:41









      Mohammad Usman

      21.4k124658




      21.4k124658










      asked Nov 23 '18 at 7:21









      Ahsan Alam SiddiquiAhsan Alam Siddiqui

      219




      219
























          3 Answers
          3






          active

          oldest

          votes


















          3














          Simply pass Object.values as callback to .map():






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer



















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:51



















          4

















          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          @HMR Thank you very much for sharing the valuable information.



          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.



          More Information






          share|improve this answer





















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

            – HMR
            Nov 23 '18 at 7:49











          • You're Correct @HMR

            – Nitish Narang
            Nov 23 '18 at 8:20






          • 1





            Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

            – Nitish Narang
            Nov 23 '18 at 8:22



















          2














          This would be Array.map and Object.values like below






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)








          share|improve this answer
























          • The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:50













          • Oh is it!!! Thanks for the info @HMR :)

            – Nitish Narang
            Nov 23 '18 at 8:17











          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%2f53442234%2fhow-to-push-all-objects-keys-to-a-separate-array-in-array-using-angular-or-javas%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









          3














          Simply pass Object.values as callback to .map():






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer



















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:51
















          3














          Simply pass Object.values as callback to .map():






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer



















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:51














          3












          3








          3







          Simply pass Object.values as callback to .map():






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          share|improve this answer













          Simply pass Object.values as callback to .map():






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          console.log(result);

          .as-console-wrapper { max-height: 100% !important; top: 0; }





          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let result = array.map(Object.values);

          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 23 '18 at 7:24









          Mohammad UsmanMohammad Usman

          21.4k124658




          21.4k124658








          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:51














          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:51








          2




          2





          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

          – HMR
          Nov 23 '18 at 7:51





          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

          – HMR
          Nov 23 '18 at 7:51













          4

















          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          @HMR Thank you very much for sharing the valuable information.



          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.



          More Information






          share|improve this answer





















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

            – HMR
            Nov 23 '18 at 7:49











          • You're Correct @HMR

            – Nitish Narang
            Nov 23 '18 at 8:20






          • 1





            Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

            – Nitish Narang
            Nov 23 '18 at 8:22
















          4

















          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          @HMR Thank you very much for sharing the valuable information.



          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.



          More Information






          share|improve this answer





















          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

            – HMR
            Nov 23 '18 at 7:49











          • You're Correct @HMR

            – Nitish Narang
            Nov 23 '18 at 8:20






          • 1





            Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

            – Nitish Narang
            Nov 23 '18 at 8:22














          4












          4








          4










          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          @HMR Thank you very much for sharing the valuable information.



          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.



          More Information






          share|improve this answer


















          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          @HMR Thank you very much for sharing the valuable information.



          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.



          More Information






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)





          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          arrayOfValues = array.map(({name, age})=> ([name, age]))

          console.log(arrayOfValues)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 14:30

























          answered Nov 23 '18 at 7:27









          Mohammed AshfaqMohammed Ashfaq

          1,7712713




          1,7712713








          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

            – HMR
            Nov 23 '18 at 7:49











          • You're Correct @HMR

            – Nitish Narang
            Nov 23 '18 at 8:20






          • 1





            Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

            – Nitish Narang
            Nov 23 '18 at 8:22














          • 2





            The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

            – HMR
            Nov 23 '18 at 7:49











          • You're Correct @HMR

            – Nitish Narang
            Nov 23 '18 at 8:20






          • 1





            Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

            – Nitish Narang
            Nov 23 '18 at 8:22








          2




          2





          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

          – HMR
          Nov 23 '18 at 7:49





          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.

          – HMR
          Nov 23 '18 at 7:49













          You're Correct @HMR

          – Nitish Narang
          Nov 23 '18 at 8:20





          You're Correct @HMR

          – Nitish Narang
          Nov 23 '18 at 8:20




          1




          1





          Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

          – Nitish Narang
          Nov 23 '18 at 8:22





          Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.

          – Nitish Narang
          Nov 23 '18 at 8:22











          2














          This would be Array.map and Object.values like below






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)








          share|improve this answer
























          • The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:50













          • Oh is it!!! Thanks for the info @HMR :)

            – Nitish Narang
            Nov 23 '18 at 8:17
















          2














          This would be Array.map and Object.values like below






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)








          share|improve this answer
























          • The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:50













          • Oh is it!!! Thanks for the info @HMR :)

            – Nitish Narang
            Nov 23 '18 at 8:17














          2












          2








          2







          This would be Array.map and Object.values like below






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)








          share|improve this answer













          This would be Array.map and Object.values like below






          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)








          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)





          let array = [
          {'name' : 'Jack', 'age':23},
          {'name': 'Robin', 'age':25}
          ];

          let res = array.map(d => Object.values(d))

          console.log(res)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 7:22









          Nitish NarangNitish Narang

          2,9701815




          2,9701815













          • The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:50













          • Oh is it!!! Thanks for the info @HMR :)

            – Nitish Narang
            Nov 23 '18 at 8:17



















          • The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

            – HMR
            Nov 23 '18 at 7:50













          • Oh is it!!! Thanks for the info @HMR :)

            – Nitish Narang
            Nov 23 '18 at 8:17

















          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

          – HMR
          Nov 23 '18 at 7:50







          The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

          – HMR
          Nov 23 '18 at 7:50















          Oh is it!!! Thanks for the info @HMR :)

          – Nitish Narang
          Nov 23 '18 at 8:17





          Oh is it!!! Thanks for the info @HMR :)

          – Nitish Narang
          Nov 23 '18 at 8:17


















          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%2f53442234%2fhow-to-push-all-objects-keys-to-a-separate-array-in-array-using-angular-or-javas%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy