Karan Karan - 1 year ago 111
Javascript Question

convert array of objects into simple array nodejs

I am creating project using nodejs. I want to convert array of objects into simple array.For example

var test = [ { id: '1111',
type: 'sdfsdf'
{ id: 'df45',
type: 'fsdsdf',

I need

var actual = [111,'sdfsdf'], ['df45','fsdsdf'].

Answer Source

I would propose this solution based on a dynamic number of keys:

var arr = test.map(function(obj){
    return Object.keys(obj). // convert object to array of keys
         reduce(function(arr, current){arr.push(obj[current]); return arr}, []); // generate a new array based on object values 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download