0

I have object of array of object

{
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}

I want to convert it to array of object:

[
  {"Full Name":"Phalla Morn"}, 
  {"Full Name":"Pum Chhenghorng"},
  {"Full Name":"Proen Pich"}
]

Please help.

Mamun
  • 66,969
  • 9
  • 47
  • 59
Lim Socheat
  • 705
  • 2
  • 12
  • 25

6 Answers6

7

Get the internal arrays with Object.values(), and flatten by spreading into Array.concat():

const arr = {"94":[{"Full Name":"Phalla Morn"}],"95":[{"Full Name":"Pum Chhenghorng"}],"99":[{"Full Name":"Proen Pich"}]};
    
const result = [].concat(...Object.values(arr));

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
2

Use Object.values to extract the values from an object, and then use [].concat(... to flatmap:

const input = {
  "94": [{
    "Full Name": "Phalla Morn"
  }],
  "95": [{
    "Full Name": "Pum Chhenghorng"
  }, {
    "Full Name": "example other"
  }],
  "99": [{
    "Full Name": "Proen Pich"
  }]
};
const output = [].concat(...Object.values(input));
console.log(output);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
2

You can try the following:

var obj = {
    "94":[{"Full Name":"Phalla Morn"}],
    "95":[{"Full Name":"Pum Chhenghorng"}],
    "99":[{"Full Name":"Proen Pich"}]
    }

var arr = Object.values(obj).map(o => o[0]);
console.log(arr)
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You can use two forEach loops and make sure that if there are multiple objects in array 94 or 95 then that is also taken into account by this dynamic code logic:

let obj = {
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}; 
let res = [];
Object.keys(obj).forEach((key)=>{
   obj[key].forEach((innerObj)=>{
     res.push(innerObj);
   }); 
});
console.log(res);

Or you can use Object.values together with the spread operator:

let obj = {
  "94":[{"Full Name":"Phalla Morn"}],
  "95":[{"Full Name":"Pum Chhenghorng"}],
  "99":[{"Full Name":"Proen Pich"}]
}; 
let res = [];
Object.keys(obj).forEach((key)=>{
    res.push(...Object.values(obj[key]));
});
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Plain JS - since Object.values is not supported by IE at all

var arr = [],
    obj =  {"94":[{"Full Name":"Phalla Morn"}],"95":[{"Full Name":"Pum Chhenghorng"}],"99":[{"Full Name":"Proen Pich"}]};

Object.keys(obj).forEach(function(k) {
  arr.push(obj[k][0])
});
console.log(arr)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Try this: it should take care of arrays with multiple objects in its value.

let data = {
      "94": [{
        "Full Name": "Phalla Morn"
      }],
      "95": [{
        "Full Name": "Pum Chhenghorng"
      },{
        "Full Name": "Ryann"
      }],
      "99": [{
        "Full Name": "Proen Pich"
      }]
    };
    
    let obj = [];
    
    Object.keys(data).map(item => {
      data[item].map(item => {
        obj.push(item);
      });
    });
    
    console.log(obj);
raihan
  • 11
  • 3