[JS Practice] Get key/value pairs as arrays

Title :

Get key/value pairs as arrays


Complete the keysAndValues function so that it takes in an object and returns the keys and values as separate arrays.


keysAndValues({a: 1, b: 2, c: 3}) // should return [['a', 'b', 'c'], [1, 2, 3]]

My solution :

function keysAndValues(data){
  var arrKey = [];
  var arrValue = [];
  var objKeys = Object.keys(data);
  for(var i=0; i< objKeys.length;i++) {
  return [arrKey, arrValue];

Solution one-line function:

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

Ref. from codewars


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s