Why Object.entries(array) and array.entries() return a different result?

I am currently learning Javascript and I can’t find the answer : Why Object.entries(array) and array.entries() return a different result ?

const game = {
  team1: 'Bayern Munich',
  team2: 'Borrussia Dortmund',
  players: [
    [
      'Neuer',
      'Pavard',
      'Martinez',
      'Alaba',
      'Davies',
      'Kimmich',
      'Goretzka',
      'Coman',
      'Muller',
      'Gnarby',
      'Lewandowski',
    ],
    [
      'Burki',
      'Schulz',
      'Hummels',
      'Akanji',
      'Hakimi',
      'Weigl',
      'Witsel',
      'Hazard',
      'Brandt',
      'Sancho',
      'Gotze',
    ],
  ],
  score: '4:0',
  scored: ['Lewandowski', 'Gnarby', 'Lewandowski', 'Hummels'],
  date: 'Nov 9th, 2037',
  odds: {
    team1: 1.33,
    x: 3.25,
    team2: 6.5,
  },
};

console.log(Object.entries(game.scored)); // return (4) [Array(2), Array(2), Array(2), Array(2)]
console.log(game.scored.entries()); // return Array Iterator {}

If someone have a resource or can explain I will be grateful !

>Solution :

Because they’re two completely different functions.

Array.prototype.entries:

returns a new Array Iterator object that contains the key/value pairs for each index in the array.

Object.entries:

returns an array of a given object’s own enumerable string-keyed property [key, value] pairs. This is the same as iterating with a for…in loop, except that a for…in loop enumerates properties in the prototype chain as well.

Leave a Reply