javascriptarraysobject

Why do I get undefined in my Javascript array output instead of the correct number?


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 the output to be like this:

 [ [ 'user1', 18273 ], [ 'user2', 92833 ], [ 'user3', 90315 ] ]

Solution

  • this should work

    
    const users = { user1: 18273, user2: 92833, user3: 90315 }
    const usersArray = Object.entries(users).map((user) => ([user[0], user[1]]));
    console.log(usersArray);