This question already has an answer here:

Given a JavaScript object, how can I convert it into an array of objects (each with key, value)?

Example:

var data = { firstName: 'John', lastName: 'Doe', email: '[email protected]' }

resulting like:

[
  { key: 'firstName', value: 'John' },
  { key: 'lastName', value: 'Doe' },
  { key: 'email', value: '[email protected]' }
]

marked as duplicate by georg arrays 2 days ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 24 down vote accepted

var data = { firstName: 'John', lastName: 'Doe', email: '[email protected]' }
var output = Object.entries(data).map(([key, value]) => ({key,value}));

console.log(output);

Inspired By this post

Using map function

var data = { firstName: 'John', lastName: 'Doe', email: '[email protected]' };

var result = Object.keys(data).map(key => ({ key, value: data[key] }));

console.log(result);
    

  • Is it an ECMAScript 6 solution? – Alexander Popov Apr 4 '16 at 19:54
  • @AlexanderPopov yes, it's arrow function – isvforall Apr 4 '16 at 19:55
  • 2
    or change the variable name to key in the map and user the object property shorthand key => ({ key, value: data[key] }) – Rob Wilkinson Apr 4 '16 at 19:57

You can just iterate over the object's properties and create a new object for each of them.

var data = { firstName: 'John', lastName: 'Doe', email: '[email protected]' };
var result = [];

for(var key in data)
{
    if(data.hasOwnProperty(key))
    {
        result.push({
            key: key,
            value: data[key]
        });
    }
}
  • up for hasOwnProperty – Roko C. Buljan Apr 4 '16 at 19:51
  • Agreed! That's what I was missing! – Rob Brander Apr 4 '16 at 19:51
  • up for hasOwnProperty but down for not doing it in a functional way, like @isvforall – Emil Oberg Apr 4 '16 at 19:55
  • @EmilOberg not doing it in a ES6 way does not mean this is a not reliable (even great and far more readable) answer. A downvote seems odd. – Roko C. Buljan Apr 4 '16 at 20:01
  • @roko-c-buljan, there's nothing ES6 about it. Object.keys and Array.map is all good old EcmaScript 5.1. (Yes the arrow function @isvforall used is ES6 but not really an interesting part of the answer, e.g see the answer by @rob-brander) – Emil Oberg Apr 4 '16 at 20:08

Just make your life easier and use es6 syntax with a map

    var output = Object.keys(data).map(key => {
      return {
        key: key,
        value: data[key]
      };
    })

The previous answer lead me to think there is a better way...

Object.keys(data).map(function(key) {
  return { key, value: data[key] };
});

or in ES6 using arrow functions:

Object.keys(data).map((key) => ({ key, value: data[key] }));
  • Error: Unexpected token : – joseantgv Sep 27 at 14:30
  • 1
    Ah, good catch; I've fixed it. I was just missing the brackets around the arrow function return value. – Rob Brander Oct 1 at 14:56
var result = [];
for(var k in data) result.push({key:k,value:data[k]});

An alternative method for doing this that works on multi level objects and does not use recursion.

var output = []

    var o = {
      x:0,
      y:1,
      z:{
        x0:{
          x1:4,
          y1:5,
          z1:6
        },
        y0:2,
        z0:[0,1,2],
      }
    }

    var  defer = [ [ o ,[ '_root_' ] ] ]
    var _defer = []


    while(defer.length){

    var current = defer.pop()

    var root    = current[1]
        current = current[0]


      for(var key in current ){

        var path = root.slice()
            path.push(key)

        switch( current[key].toString() ){
        case '[object Object]':
          _defer.push( [ current[key] , path ] )
        break;;
        default:
         output.push({
          path  : path ,
          value : current[key]
         })
        break;;
        }
      }

      if(!defer.length)
          defer = _defer.splice(0,_defer.length)
    }

[
{ path: [ '_root_', 'x' ], value: 0 },
{ path: [ '_root_', 'y' ], value: 1 },
{ path: [ '_root_', 'z', 'y0' ], value: 2 },
{ path: [ '_root_', 'z', 'z0' ], value: [ 0, 1, 2 ] },
{ path: [ '_root_', 'z', 'x0', 'x1' ], value: 4 },
{ path: [ '_root_', 'z', 'x0', 'y1' ], value: 5 },
{ path: [ '_root_', 'z', 'x0', 'z1' ], value: 6 }
]

Or go wild and make the key and value keys customizable:

module.exports = function objectToKeyValueArray(obj, keyName = 'key', valueName = 'value') {
    return Object
        .keys(obj)
        .filter(key => Object.hasOwnProperty.call(obj, key))
        .map(key => {
            const keyValue = {};
            keyValue[keyName] = key;
            keyValue[valueName] = obj[key];

            return keyValue;
        });
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.