Object to Array (an array of arrays)
Asked Answered
C

3

6

I am trying to convert an object literal into an array of arrays by using a function.

Using the two sample objects I have, the end result I'm looking for would be:

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ] from obj1

[ "shambala","walawala"] , ["foofighter","Barstool"] , ["blahblah",1382342453] ] from obj2

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
}; 

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

var piece1 = Object.keys(obj1);

var piece2 = Object.values(obj1);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 result.push([piece1[i] , piece2[i]])
 }

console.log(result)

From what I have above, I have been able to achieve:

[ ["ugh","grr"] , ["foo", "bar"] , ["blah" , 138] ] from obj1

But I am stumped about how to achieve the same output via a function.

This seems like a simple thing.

function objToArray(objectLiteral) {

var piece1 = Object.keys(objectLiteral);

var piece2 = Object.values(objectLiteral);

var result = [ ];

for (var i = 0; i < piece1.length; i++) {
 return result.push([piece1[i] , piece2[i]])
  }
 } 

console.log(objToArray(obj1))

This is what the best I can do but I keep on getting 1 and I don't know why. Other attempts I just end up with undefined.

Copybook answered 10/1, 2017 at 23:33 Comment(1)
Array#push returns the new length of the array after you've pushed an element to it.Abnegate
P
4

The problem with your code is you're using the return earlier than needed, see this working code:

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
};

var obj2 = {
  shambala: "walawala",
  foofighter: "Barstool",
  blahblah: 1382342453
};

function objToArray(objectLiteral) {
  var piece1 = Object.keys(objectLiteral);
  var piece2 = Object.values(objectLiteral);
  var result = [];
  for (var i = 0; i < piece1.length; i++) {
    result.push([piece1[i], piece2[i]])
  }
  return result;
}

console.log(objToArray(obj1));
Playmate answered 10/1, 2017 at 23:40 Comment(2)
Thank you to everyone that commented/read/contributed anything. I really appreciate it.Copybook
Tha'er, you hit the nail on the head. It was the misplaced return.Copybook
G
2

If it's supported in your environment, you could use Object.entries:

var obj1 = {
  ugh: "grr",
  foo: "Bar",
  blah: 138
};

var pairs = Object.entries(obj1);

Alternatively, you could write an entries function like this:

function entries(object) {
  const pairs = [];
  for(let key in object) {
    if(object.hasOwnProperty(key)) {
      pairs.push([key, object[key]]);
    }
  }
  return pairs;
}
Garfield answered 10/1, 2017 at 23:35 Comment(0)
F
0

Here's how I'd implement it.

function objectToArray(obj) {
    return Object.keys(obj).map(function(prop) {
        return [prop, obj[prop]];
    });
}

// ES2015
function objectToArray (obj) {
    return Object.keys(obj).map(prop => [prop, obj[prop]]);
}

View demo

Felton answered 10/1, 2017 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.