0
votes

I'm trying to change an object literal key-value pairs into an array of arrays.

For example this

var d = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
}

function objToArray(obj) {
  var arr = [];
  for (var k in obj) {
    arr.push(obj[k]);
  }
  return arr;
}

var result = objToArray(d);
console.log(result)

Should return this [['rhino', 'mozilla'], ['chrome', 8], ['engine', 'v8']]

How can I write a function to do this? Preferable without loops.

5
And what have you tried? And why not loops? - Rajesh
@Rajesh I've tried this but I know it's wrong. function objToArray(obj) { var arr = []; for (var k in obj) { arr.push(obj[k]); } return arr; } loops become hard to follow. - Alt-Rock Ninja Cowgirl
All you have to do is arr.push([k, obj[k]]). Also loops become hard because your basic are weak. If you work hard on them, it will become easy to understand loops. - Rajesh
@Rajesh thank you. Is there a more terse way to do it? Any hints would be helpful - Alt-Rock Ninja Cowgirl
I'll update Pranav's answer as he has made it wiki post. Also note, shorter code is not always better. It will compromise readability. - Rajesh

5 Answers

1
votes

Use Object.keys and Array#map methods.

Your approach updated

var d = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
}

function objToArray(obj) {
  var arr = [];
  for (var k in obj) {
    arr.push([k,obj[k]]);
  }
  return arr;
}

var result = objToArray(d);
console.log(result)

ES5

var obj = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
};

var res = Object.keys(obj).map(function(k) {
  return [k, obj[k]]
});

console.log(res);

ES6

var obj = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
};

var res = Object.keys(obj).map(x=>[x, obj[x]]);

console.log(res);

FYI : Since object properties doesn't have any order the result array order may be changed.

1
votes

While the keys of an object have no order, I suggest to keep the order with an array for the keys and map the value with Array#map.

var object = { rhino: 'mozilla', chrome: 8, engine: 'v8' }, 
    keys = ['rhino', 'chrome', 'engine'],
    result = keys.map(function (key) {
        return [key, object[key]];
    });
                      
 console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6

var object = { rhino: 'mozilla', chrome: 8, engine: 'v8' }, 
    keys = ['rhino', 'chrome', 'engine'],
    result = keys.map(key => [key, object[key]]);
                      
 console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
0
votes

you can do something like this

check this snippet

var obj = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
}
var arr = Object.keys(obj).map(function(key) {
  return [key, obj[key]];
});

console.log(arr);

Hope it helps

0
votes

var d = {
  rhino: 'mozilla',
  chrome: 8,
  engine: 'v8'
}

function objToArray(obj) {
  var arr = [];
  for (var k in obj) {
    var ls = [k,obj[k]];
    arr.push(ls);
  }
  return arr;
}

var result = objToArray(d);
console.log(result)
0
votes

Using _.zip with lodash 4.

function objToArray(obj) {
  if(!obj || typeof obj !== 'object') {
    return false;
  }
  return _.zip(_.keys(obj), _.values(obj));
}

See working version: http://jsbin.com/qokisoqapu/edit?js,console