Convert json to array using pure javascript

0

I have an object

{
         '1': {
          'nome': 'pedro',
          'idade': 2,
        },
        '2': {
          'maria': maria,
          'idade': 5,

        }
}

Array

[{
        '1': {
          'nome': 'pedro',
          'idade': 2,

        },
        '2': {
          'maria': maria,
          'idade': 5,

        }
}]

I need to check if it is an object as the example and if it is to transform into array as

pure javascript template

    
asked by anonymous 06.07.2018 / 23:19

1 answer

1
/*
 * @param {Object} nested
 * @returns {Array}
*/

function nestedToArray(nested) {
    let arrayOfObjects = [];

    for (const prop in nested) {
        if (nested.hasOwnProperty(prop)) {
            arrayOfObjects.push(nested[prop]);
          } 
    }
    return arrayOfObjects;
}

Reference: link

    
07.07.2018 / 02:57