2

I am trying to write a function that accepts an object and returns an array of arrays of key-value pairs. I also cannot use the Object.entries() function.

Example: For var obj = { a: 1, b: 2, c: 3 }; I would want to return: [["a",1], ["b",2], ["c",3]]

Here is what I have written so far:

function entries(obj) {

var result = Object.keys(obj).map(function(key) {
  return [Number(key), obj[key]];
});
}
console.log(
  entries(obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0})
  );

However, I can only get it to return undefined at this point. What exactly am I doing incorrectly here?

  • 1
    You forgot to `return` inside the `entries` function. You also shouldn't use `Number(key)`, that'll result in `NaN` for keys that can't be cast to numbers, like `'a'` – CertainPerformance Dec 30 '19 at 06:47

3 Answers3

2

You can do a simple for loop

var obj= {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var arr=[]
for(var item in obj){
  arr.push([item,obj[item]])
}
console.log(arr)
Stefan Avramovic
  • 1,365
  • 2
  • 11
  • 20
1

First let's analyse your code and detect some mistakes and then we will focus on the solution.

function entries(obj) {
  var result = Object.keys(obj).map(function(key) {
    return [Number(key), obj[key]];
  });
}
console.log(
  entries(obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0})
);

In your code the function entries does not return any value. Therefore, the console.log() will return undefined. We can fix this replacing var result = with return. So now your entries function returns an array based on your object obj. Like this:

function entries(obj) {
  return Object.keys(obj).map(function(key) {
    return [Number(key), obj[key]];
  });
}
console.log(
  entries(obj = {
    "1": 5,
    "2": 7,
    "3": 0,
    "4": 0,
    "5": 0,
    "6": 0,
    "7": 0,
    "8": 0,
    "9": 0,
    "10": 0,
    "11": 0,
    "12": 0
  })
);

Now we can optimize a little bit let's define a two objects: objA and objB. the first one will be your first example.

let objA = { a: 1, b: 2, c: 3 };
let objB = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};

If you call console.log(entries(objA)) you wil get an NaN error because the a is not a number and you are using the Number() method to cast the key values. Let's removed that function call. Thi will be final version of the solution:

let objA = { a: 1, b: 2, c: 3 };
let objB = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};

function entries(obj) {
  return Object.keys(obj).map(function(key) {
    return [key, obj[key]];
  });
}

console.log(entries(objA));
console.log(entries(objB));
Teocci
  • 7,189
  • 1
  • 50
  • 48
0

You forgot to return final result from entries()

function entries(obj) {
  var result = Object.keys(obj).map(function(key) {
      return [Number(key), obj[key]];
    });
    return result;
}
console.log(
  entries(obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0})
  );
Akash Shrivastava
  • 1,365
  • 8
  • 16