Search code examples
javascripttypescript

Push object keys and its values to array


I have an object like this:

{
 "id": 23,
 "name": "Jacob",
 "link": {
     "rel": "self",
     "link": "www.abc.com"
 },
 "company":{
       "data":{
           "id": 1,
           "ref": 324
       }
 }

I want to store each key with its value to an array in javascript or typescript like this

[
    [
        "id": 23
    ], [
        "name": "Jacob"
    ], [
        "link": { ......, ......}
    ]
] 

and so on

I am doing this so that I can append an ID for each.

My best guess I would loop through the array and append an ID/a flag for each element, which I don't know how to do as well.... how to address this issue?


Solution

  • var arr = [];
    
    for (var prop in obj) {
       if (obj.hasOwnProperty(prop)) {
          var innerObj = {};
          innerObj[prop] = obj[prop];
          arr.push(innerObj)
       }
    }
        
    console.log(arr);
    

    here is demo https://plnkr.co/edit/9PxisCVrhxlurHJYyeIB?p=preview