const users = { user1: 18273, user2: 92833, user3: 90315 }
//Answer
const usersArray = Object.entries(users).map((keys, value)=> {return keys[0] + ' ' + value[1]});
console.log(usersArray);
//OUTPUT:
['user1 undefined', 'user2 undefined', 'user3 undefined']
I expect it to be like this:
[ [ 'user1', 18273 ], [ 'user2', 92833 ], [ 'user3', 90315 ] ]
2
Answers
this should work
I agree with the example above, now the explanation.
Undefined reason:
The map function expects two arguments in the callback function: keys (the array of key-value pairs) and value (the index of the current element in the original array). In your code, you are using value as if it contained the user ID, but it actually refers to the second index of the keys array, which is always undefined.
To fix this, access the user ID from index 1 of the keys array:
now for the output format to be:
you simply set the return to []: